The task I have is:
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:
makeGuestList({ name: "Hannah Fry", age: 46 });
// should return { firstName: "Hannah", lastName: "Fry", age: 46 }
My code is thus:
function makeGuestList(person) {
const [firstName, lastName] = person.name.split(" ");
return { "firstName": firstName, "lastName": lastName };
}
const guest = { name: "Ada Lovelace" };
console.log(makeGuestList(guest));
When I run the code I get the error message:
All other properties on the object are unchanged
✕ AssertionError: expected { firstName: 'Ada', …(1) } to deeply equal { firstName: 'Ada', …(3) }
2
Answers
In your current implementation, you’re only returning an object with
firstName
andlastName
, and all other properties from the original object are not included in the returned object.To fix this, you can use the spread operator
...
to include all properties from the original object in the returned object.As an alternative to
...
spread operator, you can also use thedelete
keyword to mutate the originalperson
object argument by addingfirstName
andlastName
property, and then removename
property: