Won't Work! Lambda School Precourse: JS Assignment 12: Methods, Loops and the `this` Keyword
Cwill14 (7)

For this assignment, I can't figure out this second exercise.

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(number){
    	return number * 2;
    }
  };
  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;
}

I feel the answer should be this:

greeting: function() {
		return "Hi, my name is " + this.name;
	}

I feel like this should work, but I keep getting the warnings:
label 'greeting' on function statement. and Missing name in function declaration. After running the tests, I get these:

Exercise_One
mathHelpers object should contain a method called double
ReferenceError: exerciseOne is not defined
testName1
Should add greeting method to userObject object.
ReferenceError: exerciseTwo is not defined
More info
ReferenceError: exerciseTwo is not defined
at eval (eval at n.evaluate (https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:223:152082), <anonymous>:20:20)
at https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:670:306174
at n.run (https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:670:306398)
at n.execute (https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:670:305169)
at e.R as queueRunnerFactory
at e.execute (https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:670:263457)
at e.fn (https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:670:316285)
at https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:670:306108
at n.run (https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:670:306398)
at t (https://replbox.repl.it/public/replbox_javascript.42d3612391d31be3eb13.bundle.js:670:305769)

I am just doing the way the video (https://www.youtube.com/watch?v=FNfaU-bVdqM) and lesson taught, but it doesn't work. What's the solution?

You are viewing a single comment. View All
GoodDeedNinja (8)

I'm also stuck here :/

Cwill14 (7)

@GoodDeedNinja With everything I've tried, the problem seems to be that the key "name" is undefined. However, if you put "Juan" or "Rebecca" in as the error suggests, it says it expects the other name.

I did figure out how to get rid of the first error though, by putting it into the object but not declaring the object, as it was already declared.

userObj = {

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

UPDATE:

This finally passed for me. Using dot notation on the left side of the equation was the solution

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

SPOILER: this is the model solution:

userObj.greeting = function(){
    return `Hi, my name is ${this.name}`;
  }
GoodDeedNinja (8)

@Cwill14 Hey thanks for the reply I did the same thing and it passed. I feel like I don't fully understa d it though. I'm going to do some more studying.

wadecoplen (5)

@GoodDeedNinja There are many exercises that do not go along with the lesson. The syntax of a method according to the video is: greeting: function(){}, which is throwing the error stated above. Also, RETURN is never used in the videos, but is console.log is.. This is confusing, as console.log is not the same as RETURN.

GoodDeedNinja (8)

@wadecoplen agreed I realized that during the precourse.