Ask coding questions

← Back to all posts
JS Assignment 12: Method loops and "this" keyword help!
Iyehvah (1)

Hello i have been stuck on this problem for 2 days now i have rewatched a video that is supposed to help me with this exercise multiple times and still cant seem to get the answers right. I am just going to skip this one for now and move on. I feel i get the concept but just cant get it right lol. Some of my code is written in the code below its my "attempt" please correct me if im wrong.

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.
double: function(){
double: 4,
this.double*2; (not sure how im supposed to do it...)
}
};
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.
greeting: function(){
console.log('Hi, my name is ' + this.name);
}

// Please write all of your code on the lines above.
return userObj;
}

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

In exercise one, you have to use the proper way to return the double:

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

In exercise two, the greeting should not be logged, it should be returned. like this:

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

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

Iyehvah (1)

@vedprad1 Gah thanks man i appreciate it a lot.

vedprad1 (694)

@Iyehvah : Could you mark my answer as correct? Thanks!

mwilki7 (152)

@vedprad1 ill upvote if it helps 😂

Iyehvah (1)

@vedprad1 my bad didnt understand what you said till i read the comment above saying "ill upvote it if it helps". Didnt realize you could upvote comments. lol