Remove & Replace properties within an object

Hello,

I have a challenge that i’m trying to wrap my head around.

The project is a Guest List, where one of the object properties is the guests full name with a space between first & last name. I’ve been tasked to remove the name property, replace it with firstName and lastName which will separate the guests first and last names, but it doesn’t affect the other properties inside the object.

This is what i’ve managed so far:

function makeGuestList(person) {
// Your code goes here…

    person.firstName = person.name.split(' ')[0];
    person.lastName = person.name.split(' ').pop();

return {firstName, lastName, age}
}

This is the result im receiving so far:

Returns an object with the name property replaced by firstName & lastName properties

✓  Well done!

All other properties on the object are unchanged

✕ AssertionError: expected { firstName: ‘Ada’, …(1) } to deeply equal { firstName: ‘Ada’, …(3) }

I dont want it to be simply solved for me, I need to learn with this and would love some form of guidance/input if possible please. It seems to be telling me that i’ve managed to separate the names, but it looks like im being told that its affected the age property too.

Thank you so much

please give a link to the challenge.

I haven’t got a link as it needs a login etc, but here is the full challenge explanation:

Making the Guest List

We need to keep track of the party guests, but the data we currently have combines the guests first and last names into a single name. You have been asked to separate the names to make the data easier to work with.

The makeGuestList function takes an object with a name property whose value will be a string consisting of a first name and a last name, separated by a space. The function should return an object .

The function should remove the name property, replace it with firstName and lastName properties, as shown in the examples below.

Examples:

makeGuestList({ name: "Hannah Fry", age: 46 });
// should return { firstName: "Hannah", lastName: "Fry", age: 46 }

makeGuestList({ name: "Paul Erdős", age: 46 });
// should return { firstName: "Paul", lastName: "Erdős", age: 46 }

Note: all other properties should remain unchanged.

if you provide the link it would be much simple for us to help you,be sure to give it other time.
well,i think the problem in your code is when you return the object in the function,the key that you give are both not defined and do not have a value(property),you can not use like your code because the key and value are not the same,that is why the error is giving you the error to deeply equal because the value is not defined in your case. the correct way to do it may look like:-

return {firstName: person.firstName,
                lastName:  person.lastName,
                age: person.age};

or you can do it like this:-

function makeGuestList(person) {
// Your code goes here…
 person.firstName = person.name.split(' ')[0]; // add new property 'firstName' to person 
 person.lastName = person.name.split(' ').pop();// add new property 'lastName' to person 
delete person.name; // remove 'name' property
return person; 
}

Thank you, the second solution worked. It all makes sense too, so thanks for explaining it as well as you did :blush:

did the first solution not worked for you?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.