I was given a task, and I must be missing something. The provided code isn't the original question but similar. I have to count how many people in the array are aged 16 or over. I have played around with it and I can't work it out. Please, can someone explain what I am doing wrong?
In the task, I am given an array of objects:
var people = [{name:'Emma', age:15},{name:'Matt', age: 16}, {name:'Janet', age:17}]
I need to complete a function to count how many people are aged 16 or over. The start of the function is given (i.e. function correctAge(people){ //Complete }
)
The 'Example Code' is some skeleton code I have been playing around with. The 'Incorrect Attempt' is my attempt which is the code I keep coming back to or a variation of which is also correct...
Please Help
Incorrect attempt:
var people = [
{name: "Emma", age: 15},
{name: "Matt", age: 16},
{name: "Tom", age: 17}
];
function correctAge(array) {
// Complete the function to return how many people are age 16+
var count = 0;
for (let i = 0; i < array.length; i++) {
var obj = array.length[i];
for (prop in obj) {
if (prop[obj] >= 16) {
count++;
}
}
return count;
}
}
console.log(correctAge(people));
Example code:
var people = [
{name: "Emma", age: 15},
{name: "Matt", age: 16},
{name: "Tom", age: 17}
];
function correctAge(people) {
// Complete the function to return how many people are age 16+
}