-1

I am new at Javascript. I am currently looking at the keyword this and methods and how to return strings. I am struggling to return a string using the keyword this.

I have successfully created code that returns the string, but the problem is that it shows the error that "the object is already defined".

Here is the exercise I am working on and also the code I have tried to create which fails to return the correct results:

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.

  let userObj = {
    name: "Lisa",
    greeting: function() {
      return "Hi, my name is " + this.name;
    },
  };
  console.log(userObj.greeting());
}

//In the first line of code it shows a error which says that "userObj" is already defined. So I do not know how to return the string without creating a new object and creating a key called name.

//Here is another option I tried but it also did not work out:

function greeting() {
  this.name = "Lisa";
  let result = "Hi, my name is " + this.name;
  return result;
},
userObj.greeting();
}

//The exercise should add a greeting method to userObject object.

So if userObj has a name: 'Lisa', greeting should return: 'Hi, my name is Lisa'

Charlie Wallace
  • 1,686
  • 1
  • 14
  • 17
  • 4
    This question has nothing to do with "loops". – Pointy May 30 '19 at 16:49
  • It's literally what it says. `let`, `var`, and `const` declare variables. However, inside a function, a variable can be declared by naming a parameter. In your function you have `userObj` as a parameter, then later in your code you try to declare it again using `let`. It's already defined. Either pass in `userObj` to your function or declare it in your function code, not both. – zfrisch May 30 '19 at 16:52
  • Possible duplicate of [How does the "this" keyword work?](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – Shireesha Parampalli May 30 '19 at 16:57

3 Answers3

1

The problem is that your local variable has the same name as the function parameter. You're supposed to add a method to the existing variable, not create a new variable. The instructions specifically say "DO NOT create a new object", yet that is what you did.

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.
  
  userObj.greeting = function() {
    return "Hi, my name is " + this.name;
  };
  console.log(userObj.greeting());
}

let obj = {
  name: "Joe"
};
exerciseTwo(obj);
Barmar
  • 596,455
  • 48
  • 393
  • 495
1
function exerciseTwo(userObj){ // The argument for this "exerciseTwo" function has been declared as "userObj"

  let userObj = { // Here you are trying to declare another variable "userObj"
    name: "Lisa",
    greeting: function() {
      return "Hi, my name is " + this.name;
    }
  };

  console.log(userObj.greeting());
}

To solve your issue, - Declare the let userObj = { ... } block outside the "exerciseTwo" function and pass it in as a variable

let lisa = {
  name: "Lisa"
};

function exerciseTwo(userObj){ // Whatever variable you pass into this function will be synonymous to `userObj` within this function
  userObj.greeting = function () {
    return "Hi, my name is " + this.name;
  }
  console.log(userObj.greeting());
}

exerciseTwo(lisa) // lisa will take the position of `userObj` above
drsoph
  • 36
  • 4
0

As the exercise says you only have to add the greeting function to the users object. Like this:

let userObj = { name: "Lisa" };

function exercise2(userObj) {
    userObj.greetings = function () {
        return "Hi, my name is " + this.name;
    }
}

exercise2(userObj);

console.log(userObj.greetings());
JDuwe
  • 478
  • 5
  • 14