Couple of questions here. This is from Wes Bos' 30 Days of JavaScript course.
- Why does the ternary operator (commented out) make this false?
- Why is my solution incorrect?
Challenge
const people = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 }
];
// Array.prototype.some() // is at least one person 19 or older?
// Array.prototype.every() // is everyone 19 or older?
Wes' Solution
const isOlderThan19 = people.some(function(person) {
const currentYear = new Date().getFullYear();
// (currentYear - person.year >= 19) ? true : false
if (currentYear - person.year >= 19) {
return true;
}
});
My Solution
const isOlderThan19 = (element) => {
let age = new Date().getFullYear() - element.year;
age >= 19;
}
console.log(people.some(isOlderThan19));
I tried logging the ages that are being set to the console. All but one of the people are over 19 but it returns false. I'm trying to understand why.