Skip to content

Instantly share code, notes, and snippets.

@elGuille-info
Created November 30, 2022 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elGuille-info/2543357b8a11fdb9e14080a458fb448c to your computer and use it in GitHub Desktop.
Save elGuille-info/2543357b8a11fdb9e14080a458fb448c to your computer and use it in GitHub Desktop.
Manipulación de arrays en JavaScript
/*
Del ejemplo de [Active learning: Printing those products](
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Arrays?qs=arrays#active_learning_printing_those_products)
*/
//const list = document.querySelector('.output ul');
//const totalBox = document.querySelector('.output p');
let total = 0;
//list.innerHTML = '';
//totalBox.textContent = '';
const products = [
'Underpants:6.99',
'Socks:5.99',
'T-shirt:14.99',
'Trousers:31.99',
'Shoes:23.99',
];
//const listItem = [];
console.log("Los productos son:")
for (const product of products) {
const subArray = product.split(':');
const name = subArray[0];
const price = Number(subArray[1]);
total += price;
const itemText = `${name} — $${price}`;
//const listItem = document.createElement('li');
//listItem.textContent = itemText;
//list.appendChild(listItem);
//listItem.push(itemText);
console.log(itemText);
}
console.log();
//totalBox.textContent = `Total: $${total.toFixed(2)}`;
console.log( `Total: $${total.toFixed(2)}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment