Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTadjiev Codes
9,626 PointsUsing forEach, turn the number strings from the stringPrices array into floats and add them all, storing the total in th
I don't know why it doesn't work?
const stringPrices = ['5.47', '3.12', '8.00', '5.63', '10.70'];
let priceTotal = 0;
// priceTotal should be: 32.92
// Write your code below
stringPrices.forEach(price => {
const prices = parseFloat(stringPrices);
priceTotal += price;
});
2 Answers
Victor Mercier
14,667 PointsHi, let's breakdown your code. You are iterating over all elements in the array. Then, you store in a constant prices a parseFloat() of all elements in the array, which is not correct because we wanna parse the current item.
What you want to do is forEach item, parse this item and adds it to the totalPrice variable. Here is how I would tackle that :
const stringPrices = ['5.47', '3.12', '8.00', '5.63', '10.70'];
let priceTotal = 0.0;
stringPrices.forEach(price=>{
const parsedPrice = parseFloat(price);
priceTotal += parsedPrice;
});
If it helped you, please mark as best answer!
Henry Blandon
Full Stack JavaScript Techdegree Graduate 21,521 PointsHere is anther way, just shorter version
stringPrices.forEach(price=> priceTotal+=parseFloat(price));