Ask coding questions

← Back to all posts
Js: Methods, Loops, and the 'this' statement.
Paultheriault (2)

function exerciseOne(){
// Exercise One: In this exercise you are given an object called 'mathHelpers'
// Within mathHelpers, create a method called 'double'
// This method should take one parameter, a number,
// and it should return that number multiplied by two.
let mathHelpers = {
// Create double method in here.

};
return mathHelpers;
}

function exerciseTwo(userObj){
// Exercise Two: You will be given an object called 'userObj'
// userObject will already have a key on it called 'name'
// Add a method to userObj, called 'greeting'.
// Using the keyword 'this', the greeting method should return the following string:
// 'Hi, my name is ' and the users name.
// eg: If userObj has a name: 'Dan', greeting should return: Hi, my name is Dan'
// NOTE: DO NOT create a new object.
// NOTE: DO NOT create a key called name the key is already on the object.

// Please write all of your code on the lines above.
return userObj;
}
Feel like I am hitting a wall with these ones, can anyone please help. Thank you

Answered by vedprad1 (732) [earned 5 cycles]
View Answer
Commentshotnewtop
vedprad1 (732)

Here are the answers. For more resources, check w3schools.com.

function exerciseOne() {
  let mathHelpers = {
    double : function (num) {
      return num * 2;
    }
  }
}

function exerciseTwo(userObj) {
  userObj.greeting = function () {
    return 'Hi, my name is ' + this.name;
  }
}

If this answers your question, please check the checkmark on the left side of this message. Thanks and Good Luck!

Paultheriault (2)

@vedprad1 Thanks for your help. I got them all wrapped up.