I am relatively new to coding and working through a bootcamp right now. I will summarize the task at hand and then post the code below and my console result. If anyone can help me see what I'm doing wrong that would be greatly appreciated!
Task: loop through the array and use the age of the people in the array to determine if they're old enough to see Mad Max.
Context: earlier in my work, I was getting at least some output, not getting different results for different ages in the array (getting all "not old enough"), but there was at least something being displayed in the console. Now all I get is this:
node "/Users/me/Dev/vs intro/10-Loops-and-Arrays-Practice/loopsArrays.js"
Here is my code:
var peopleWhoWantToSeeMadMaxFuryRoad = [
{
name: "Mike",
age: 12,
gender: "male"
},{
name: "Madeline",
age: 80,
gender: "female"
},{
name: "Cheryl",
age: 22,
gender: "female"
},{
name: "Sam",
age: 30,
gender: "male"
},{
name: "Suzy",
age: 4,
gender: "female"
}
];
for (var i = 0; i < peopleWhoWantToSeeMadMaxFuryRoad.length; i++){
if (peopleWhoWantToSeeMadMaxFuryRoad.age >= 18){
console.log("is old enough to see Mad Max")
} else if (peopleWhoWantToSeeMadMaxFuryRoad.age <= 17){
console.log("is not old enough to see Mad Max")
}
};
Thank you again to anyone who can help!
You need to get the current item from the array before you operate on it.
peopleWhoWantToSeeMadMaxFuryRoad
is the array, so it doesn't have an age
. The items inside the array have an age
.
Doing something like the following should get you some output:
for (var i = 0; i < peopleWhoWantToSeeMadMaxFuryRoad.length; i++){
let cleverNameHere = peopleWhoWantToSeeMadMaxFuryRoad[i];
if (cleverNameHere.age >= 18){
console.log("is old enough to see Mad Max")
} else if (cleverNameHere.age <= 17){
console.log("is not old enough to see Mad Max")
} else {
console.log("IDK what went wrong here, but it's a doozy")
}
}