# Halp!

I need help with an exercise that I’m having a hard time with. I’ve been searching online for guidance and I haven’t come up with anything! Could someone point me in the right direction? Even if it’s to just let me know what method(s) I need to study to solve this puzzle. Thanks in advance!

Instructions: Modify the function to return the number of drivers age 16 and over. It should console log the number 2.

``````function countLegalDrivers(people) {

}

/* Do not modify code below this line */

const examplePeopleArray = [
{ name: 'John', age: 15 },
{ name: 'Jane', age: 16 },
{ name: 'Jack', age: 17 }
];

console.log(countLegalDrivers(examplePeopleArray));
``````

First, you need to iterate through the `examplePeopleArray`. You could use a simple:
`for (let i =0; i < people.length; i++)` or a `forEach` method if you are familiar with it. Then you’ll need to access the age property of each object in the examplePeopleArray, and test the age to determine if it is 16 or higher. Check out these links, and if you still need more guidance, feel free to ask. Good luck!

Ahh I see… I was close with the iteration but I missed the mark on accessing the age property of each object. I will check out your links and try again. Thanks!

1 Like

Hey it’s me again… so I’ve checked out the links and they were somewhat helpful. But I’m still having trouble with this. I tried doing it this way:

``````function countLegalDrivers(people) {

for(i = 0; i < people.length; i++) {
return people[i]

}
}
let count = 0
if(examplePeopleArray.age[0] || examplePeopleArray.age[1] >= 16 && examplePeopleArray.age[2] >= 16){
console.log(count++);
}
``````

And I am getting the error message - ReferenceError: examplePeopleArray is not defined
The fact that examplePeopleArray is a const is also not letting me declare it in the function. When I try to create examplePeopleArray in the function I get the message - SyntaxError: Identifier ‘examplePeopleArray’ has already been declared. I guess this is due to examplePeopleArray being declared as a const in the part of the lesson I’m not allowed to modify. I’ve been all over the internets but I’m not sure how to circumvent this

``````function countLegalDrivers(people) { //remember that people is now the same as the examplePeopleArray, so inside the function we just use people
//All of your code should go here inside the function
//I'll help get you started by re-arranging some of your code
let count = 0;
for(let i = 0; i < people.length; i++) {
if(/* this is where you'll need to check if the age is >= 16 */) {
count++;  //if they are 16 or older, we increment the count variable
}
}
return //you need to return the number of people who are 16 or older
}

/* Do not modify code below this line */

const examplePeopleArray = [
{ name: 'John', age: 15 },
{ name: 'Jane', age: 16 },
{ name: 'Jack', age: 17 }
];

console.log(countLegalDrivers(examplePeopleArray)); // this line calls or causes the computer to execute the countLegalDrivers function and passes or sends the examplePeopleArray to the function as an argument
// when the function is finished, the return statement at the end will send the number
// of drivers 16 or older back to here, and that result will be logged/printed to the console
``````

To access the age property of each individual inside the examplePeopleArray you use the following syntax: `array[index].property` for example: `people[i].age`

Hopefully this will help.

1 Like

This is so incredibly helpful! I’m just a few months into learning javascript and I feel very lost at times lol. Many thanks again

1 Like

Thank you so much man, been trying for so much time now this code

1 Like

For anyone needing help on this, I believe I solved it using the steps given:

//MODIFY THE FUNCTION TO RETURN THE COUNT OF HOW MANY PEOPLE ARE 16 OR OLDER.

function countLegalDrivers(people) {
let count = 0;
for(let i = 0; i < people.length; i++) {
if(people[i].age >= 16) {
count++;
}
}
return count
}
/* Do not modify code below this line */

const examplePeopleArray = [
{ name: ‘John’, age: 15 },
{ name: ‘Jane’, age: 16 },
{ name: ‘Jack’, age: 17 }
];

console.log(countLegalDrivers(examplePeopleArray), ‘<-- should be 2’);