DébutantNon commencé

Exercice 6.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(fruit => {
  console.log(fruit.nom + " coûte " + fruit.prix + " CHF")
})
// "Pomme coûte 2 CHF"
// "Banane coûte 1.5 CHF"

Comment ça marche ?

  1. forEach parcourt chaque élément du tableau
  2. À chaque tour, l'élément est passé à la fonction
  3. On accède aux propriétés avec la notation point

Bon à savoir : forEach ne modifie pas le tableau et ne retourne rien. Il sert uniquement à exécuter une action pour chaque élément (comme afficher).

📝 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

Lis bien les instructions et lance-toi !

Indices (0/3)

Indice 1 verrouillé
Indice 2 verrouillé
Indice 3 verrouillé