I've been advised to use a specific code for one of the problems I tackled some time ago. I'm attempting to figure it out but so far, I haven't made any progress. The challenge is to accomplish this task using replace(), map() and more within replit while not altering the entire array as part of the 'For Fun' challenge.
const products = [
{
priceInCents: 3995,
},
{
priceInCents: 2500,
},
{
priceInCents: 8900,
},
{
priceInCents: 12500,
},
];
/* Now trying to use:
products[i].priceInDollars = $${(products[i].priceInCents * .01).toFixed(2)}
*/
/*
New
Code
*/
function addPriceInDollarsKeyToProducts(pricey)
{ for (let i = 0; i < products.length; i++)
{ for (let product = products[i];
product.priceInDollars = `$${(product.priceInCents * .01).toFixed(2)}`; )
break;
}
}
addPriceInDollarsKeyToProducts();
console.log(products)
On running this snippet, it appears to function correctly.
For instance: I expect products[0].priceInDollars to be "$39.95"
, however, it returns '$39.95',
. The snippet interprets it as "$39.95"
.
The goal is not to completely recreate the entire array. If the code doesn't abide by the quotation requirements, a
TypeError: Cannot read property '0' of undefined
occurs.
edited for clarity