DébutantNon commencé
Exercice 4.5 · 8 min · 0 tentative
Parcourir des objets
Objectif
Utiliser forEach pour afficher tous les éléments d'un tableau d'objets.
Contexte
La méthode forEach permet de parcourir un tableau :
const fruits = [
{ nom: "Pomme", prix: 2 },
{ nom: "Banane", prix: 1.5 }
]
fruits.forEach(function(fruit) {
console.log(fruit.nom + " coûte " + fruit.prix + " CHF")
})
// "Pomme coûte 2 CHF"
// "Banane coûte 1.5 CHF"
Comment ça marche ?
forEachparcourt chaque élément du tableau- À chaque tour, l'élément est passé à la fonction
- On accède aux propriétés avec la notation point
Consigne
Parcours le tableau jeux et affiche chaque jeu au format :
Zelda : 9.5/10
Mario : 9.0/10
Pokemon : 8.5/10
Objectifs
- Utiliser forEach
- Afficher Zelda
- Afficher Mario
- Format avec /10
Lisez bien les instructions et lancez-vous !
Indices (0/3)
Indice 1 verrouillé
Indice 2 verrouillé
Indice 3 verrouillé