I have a question. I should write a function which will print the oldest people name from an array and if the date of birth is missing it should count from the current year. I have managed to print the oldest people name but when I write the function for the people whose date of death is missing it prints the wrong name. I have done so far
const people = [
{ name : "Ani", dateBirth: 1970, dateDeath: 2019 },
{ name : "Anna", dateBirth: 1950, dateDeath: 2015 },
{ name : "Ashot", dateBirth: 1550 },
];
function findOldestPeople(people){
for(let i of people){
if(!i.hasOwnProperty("dateDeath")){
let countmissingdate = new Date().getFullYear() - people[2].dateBirth
console.log(countmissingdate)
let z = people.sort((a,b)=> b.dateDeath - a.dateBirth)
document.write("The oldest people is: " + z[0].name)
}
}
}
findOldestPeople(people)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script src="script.js"></script>
</html>
.