¡Nuevo en JS! Intentando especificar una declaración para una de mis claves dentro de mi objeto, que nuevamente está anidado en una matriz. Junto a Slowbro, debería aparecer el mensaje: "Wow, este es un Pokémon grande", pero no funciona. Estoy tratando de escribir un condicional general dentro de mi ciclo for para todos los Pokémon, pero también especifico que el mensaje "Wow, este es un gran Pokémon" solo debería aparecer para Slowbro y no solo para cualquiera que cumpla con los criterios de altura> 1.5. ¿Qué me perdí?
//Array contains Pokemon data to display in application //Each object represents one Pokemon with respective properties let pokemonList = [ {name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']}, {name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']}, {name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']} ]; //Loop lists each Pokemon in array by assigning name and height keys for (let i=0; i < pokemonList.length; i++) { document.write(pokemonList[i].name+ ' (height: ' + pokemonList[i].height + ') ' + '<br>') } for (let i=0; i < pokemonList[i].length; i++) { if (pokemonList[i].height > 1.5 && pokemonList[i].name === 'Slowbro') { console.log('- Wow, this is a big Pokemon!'); } else if (pokemonList[i].height > 0.5 && pokemonList[i].height < 1.5) { console.log('- This is an average size Pokemon.'); } else { console.log('- This is a small Pokemon.') } }Prueba esto
//Array contains Pokemon data to display in application //Each object represents one Pokemon with respective properties let pokemonList = [ {name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']}, {name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']}, {name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']} ]; const html = pokemonList.map(({name, height}) => { return `${name} (height: ${height})` }).join('<br>') document.write(html) pokemonList.forEach(({name, height}) => { if( height > 1.5){ console.log('- Wow, this is a big Pokemon!') }else if(height > 0.5){ console.log('- This is an average size Pokemon.'); } else { console.log('- This is a small Pokemon.') } })En el último bucle for en su fragmento de código, en lugar de la longitud de la matriz, está utilizando la longitud de su primer elemento:
for (let i=0; i < pokemonList[a].length; i++)Donde probablemente desee la longitud de la matriz en sí:
for (let i=0; i < pokemonList.length; i++)en cambio.
//Array contains Pokemon data to display in application //Each object represents one Pokemon with respective properties let pokemonList = [ {name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']}, {name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']}, {name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']} ]; //Loop lists each Pokemon in array by assigning name and height keys for (let i=0; i < pokemonList.length; i++) { document.write(pokemonList[i].name+ ' (height: ' + pokemonList[i].height + ') ' + '<br>') } for (let i=0; i < pokemonList.length; i++) { if (pokemonList[i].height > 1.5 && pokemonList[i].name === 'Slowbro') { console.log('- Wow, this is a big Pokemon!'); } else if (pokemonList[i].height > 0.5 && pokemonList[i].height < 1.5) { console.log('- This is an average size Pokemon.'); } else { console.log('- This is a small Pokemon.') } }