Skip to content

Conversation

@jonwyuen
Copy link

@jonwyuen jonwyuen commented Aug 1, 2017

updated testing.js and testingSpec.js with solutions


//Fix

var obj = {
Copy link
Contributor

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){
Copy link
Contributor

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 = {
Copy link
Contributor

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]
Copy link
Contributor

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 = "";
Copy link
Contributor

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) {
Copy link
Contributor

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];
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants