1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
const shoppingList = [{
product: 'headphone',
quantity: 25,
price: 2999
},
{
product: 'earphone',
quantity: 28,
price: 110
},
{
product: 'speakers',
quantity: 14,
price: 98
}
];
add.addEventListener('click', function() {
if (shoppingList[0]) {
// On ajoute un produit à chaque clique
items.textContent++;
// On enlève à chaque fois une quantité du produit
shoppingList[0].quantity--;
console.log(shoppingList[0].quantity);
if (shoppingList[0].quantity === 0) {
// ICI stopper l'ajout
// items.textContent === 25, soit le maximum possible
}
// On stocke le prix total en fonction du nombre d'article choisi
const totalPrice = parseInt(price.textContent) + shoppingList[0].price;
// Le prix affiché correspond au nombre de produit choisi
price.textContent = totalPrice;
numberOfProducts.textContent = 'x' + items.textContent;
numberOfProducts.style.color = '#000';
headphoneMarkTwoImg.style.display = 'block';
product.textContent = 'xx99 mk ii';
}
}); |
Partager