Home > Software engineering >  Trouble changing object data with new values
Trouble changing object data with new values

Time:09-15

I've started learning JavaScript today and I'm having trouble changing object information to new values returning in a new string. Here is what I've got below;

        function newSentence(arg) {
        person = {
        name : 'Billy',
        age : 666,
        job : 'Bob'
        };
        let pikachu = `Hi my name is ${person.name}, I am ${person.age} years old and I am a ${person.job}`;
        return pikachu, arg;
    }

console.log(pikachu)

The objective I want is to change the object values to a new name, age and job passed through using a function. For example: newSentence({ name: 'Sara', age: 18, job: 'larper' }); should return "Hi my name is Sara, I am 18 years old and I am a larper"

I've had a quick google my I think my dyslexia is getting in the way. Please help.

CodePudding user response:

In your code you are hardcoding a person object with values and using that in the template string, hence the values you are sending to the function are not being used

Try this code

function newSentence(person) {
    return `Hi my name is ${person.name}, I am ${person.age} years old and I am a ${person.job}`;
}

console.log(newSentence({ name: 'Sara', age: 18, job: 'larper' }));

Gives Output

Hi my name is Sara, I am 18 years old and I am a larper
  • Related