Arrays active learning's code

Greetings, I am glad to be here.

I am just now at the Arrays learning, trying the ’ [Active learning: Printing those products!’ assignment.

I regularly use the Show Solution option, but I would really appreciate feedback on my code, which I’m pretty sure wouldn’t even work properly (regardless of the live output, which sometimes tends to break for me, even when I do enter correct solutions code).

const list = document.querySelector(’.output ul’);
const totalBox = document.querySelector(’.output p’);
let total = 0;
list.innerHTML = ‘’;
totalBox.textContent = ‘’;
// number 1
let products = [‘Underpants:6.99’,

// number 2
for (let i = 0; i < products.length; i++) {
// number 3
let prices = products[i].split(’:’);
let stringPrices = Number(prices);

for (let z = 0; z < stringPrices.length; z++) {
// number 4
let total[z] += stringPrices

// number 5
let itemText = ${products[i] — ${prices[i]} ;

const listItem = document.createElement(‘li’);
listItem.textContent = itemText;

totalBox.textContent = ‘Total: $’ + total.toFixed(2);

Thanks in advance to all helpers!

As a sidenote, I would really appreciate if someone could tell me how to hide my email address from appearing on my profile and posts.

Hello @troubled

you are very close

products[i].split(’:’);split return an array for all words splited by the : so it will return an array of 2 element the product name and the price so prices variable are array not a number so you need to use the second element of the array prices like that
stringPrices = Number(prices[1]) as the second element is the price

total should be sum of all prices so you should not use it array but as number so it be total += stringPrices; by the way total already defined just 2 line before the array element on that line let total = 0; so you just use it and do not use the second for loop that around this statement no need for that also

for the itemText you need to use the product name and price

 let itemText =  `${products[i].split(":")[0]} — $${stringPrices}`;

hope that help and have a nice day :slight_smile:

1 Like

i do not see your email just your username

1 Like