Search code examples
javascriptjavascript-objects

Two objects javascript. I am bit confused


I'm a bit confused with JavaScript's objects. I want the following :

function to accept as the first parameter the object person, and as the second parameter the name field. It will modify the person object, in which the field (property) name of the object person to receive the value that passed as the second parameter. The function will not return anything, it will modifies the object directly.

Take the following piece of code:

let person = {
  name: "name",
};

let name = {
  name: "name_02",
};

let b = person === name;

function setName(person, name) {}
console.log(b);

I am sure that my code is miserable because I am a beginner, please help me with clarifications and your understanding.


Solution

  • Your not a million miles away. The question is asking you to create a function that can be called on a person object, and set it's name using the second parameter.

    So you created the function with the 2 params, but just have not done anything with it yet.

    Not sure what your trying to do with the comparison part, as that's not in the question anyway.

    Your probably after something like ->

    let person = {
      name: "name",
    };
    
    function setName(person, name) {
      person.name = name;
    }
    
    console.log(person);
    setName(person, 'Bob');
    console.log(person);