-
Notifications
You must be signed in to change notification settings - Fork 83
Testing Exercises Solutions #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…pt_exercises merging upstream
|
|
||
| //Fix | ||
|
|
||
| var obj = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way to fix it is:
var obj = {
fullName: "Harry Potter",
person: {
sayHi: function() {
return "This person's name is " + this.fullName;
}
}
};
obj.person.sayHi = obj.person.sayHi.bind(obj);
| //3. Write a function called sumEvenArguments which takes all of the arguments passed to a function and returns the sum of the even ones. | ||
|
|
||
| function sumEvenArguments(){ | ||
| return [].slice.call(arguments).reduce(function(acc,next){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the use of reduce!
| } | ||
| } | ||
|
|
||
| var person = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be simplified to:
let person = {
fullName: "Harry Potter",
sayHi() {
setTimeout( () => console.log(`Your name is ${this.fullName}`), 1000);
}
}
|
|
||
| var arr = [1,2] | ||
| var [temp,temp2] = arr | ||
| [temp,temp2] = [temp2,temp] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var arr = [1,2];
arr = [arr[1], arr[2]];
|
|
||
|
|
||
| String.prototype.reverse = function(){ | ||
| let reverseStr = ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try splitting the string so that you don't use so much memory.
| } | ||
| } | ||
|
|
||
| Array.prototype.reduce = function(fn, startVal) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
| } | ||
|
|
||
| Array.prototype.reduce = function(fn, startVal) { | ||
| let acc = startVal ? startVal : this[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is problematic because the startVal could be something that is falsy in javascript like 0 or "". The better approach would be to check the length of the arguments object.
updated testing.js and testingSpec.js with solutions