Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why I can't return an object with an added property key in javaScript

You have been asked to update the visitors list with the amounts that each guest will donate for the organisation . Unfortunately, all the donation amounts found in email messages have been sent to you as strings!

Examples:

trackVisitors({ firstName: "Danciaa", lastName: "fran", age: 21 }, ’24’);
// should return { firstName: "Danciaa", lastName: "fran", age: 21, paidForTicket: 24 }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you’re fine with mutating the original object, you can do this:

const trackVisitors = (visitor, paidForTicket) => {
  visitor.paidForTicket = +paidForTicket; // or `Number()` or `parseInt()` or various other methods of string->int
  return visitor;
}
console.log(trackVisitors({
  firstName: "Danciaa",
  lastName: "fran",
  age: 21
}, '24'));

If you’re making a pure function, you can do this instead:

const trackVisitors = (visitor, paidForTicket) => ({
  ...visitor,
  paidForTicket: +paidForTicket, // or `Number()` or `parseInt()` or various other methods of string->int
});
console.log(trackVisitors({
  firstName: "Danciaa",
  lastName: "fran",
  age: 21
}, '24'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading