done find the oldest exercise

This commit is contained in:
aidwiz@yahoo.com 2019-10-07 20:06:21 +08:00
parent bccd35b40d
commit 9774d20102
4 changed files with 131 additions and 4 deletions

View File

@ -1,5 +1,52 @@
let findTheOldest = function() {
let findTheOldest = function(people) {
let userData = [];
for (i = 0; i < people.length; i++) {
let date = new Date();
let aliveStatus = "alive";
let personAge;
// if alive check age (today - yearOfBirth)
// if not check age (yearOfDeath - yeahOfBirth)
if ("yearOfDeath" in people[i] === false) {
// aliveStatus = "alive";
console.log(date.getFullYear());
console.log(people[i].yearOfBirth);
personAge = Number(date.getFullYear()) - people[i].yearOfBirth;
console.log(personAge);
} else {
personAge = people[i].yearOfDeath - people[i].yearOfBirth;
aliveStatus = "dead";
}
let personObject = {
name: people[i].name,
age: personAge,
status: aliveStatus
};
userData.push(personObject);
}
console.log(userData);
let oldestPersonName = userData[0].name;
for (i = 0; i < userData.length; i++) {
if (i === 0) {
continue;
} else {
if (userData[i].age > userData[i - 1].age) {
oldestPersonName = userData[i].name;
oldestPersonAge = userData[i].age;
console.log("current oldies : " + oldestPersonName);
}
}
}
personObject = { name: oldestPersonName, age: oldestPersonAge };
//console.log(personObject);
return personObject;
};
}
module.exports = findTheOldest

View File

@ -21,7 +21,7 @@ describe('findTheOldest', function() {
]
expect(findTheOldest(people).name).toEqual('Ray');
});
xit('finds the oldest person if someone is still living', function() {
it('finds the oldest person if someone is still living', function() {
const people = [
{
name: 'Carly',
@ -40,7 +40,7 @@ describe('findTheOldest', function() {
]
expect(findTheOldest(people).name).toEqual('Ray');
});
xit('finds the oldest person if the OLDEST is still living', function() {
it('finds the oldest person if the OLDEST is still living', function() {
const people = [
{
name: 'Carly',

12
findTheOldest/test.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="test.js"></script>
</body>
</html>

68
findTheOldest/test.js Normal file
View File

@ -0,0 +1,68 @@
function findTheOldest (people) {
// console.log(people);
let userData = [];
for (i = 0; i < people.length; i++) {
let date = new Date();
let aliveStatus = "alive";
let personAge;
// if alive check age (today - yearOfBirth)
// if not check age (yearOfDeath - yeahOfBirth)
if ('yearOfDeath' in people[i] === false) {
// aliveStatus = "alive";
console.log(date.getFullYear());
console.log(people[i].yearOfBirth);
personAge = Number(date.getFullYear()) - people[i].yearOfBirth;
console.log(personAge);
} else {
personAge = people[i].yearOfDeath - people[i].yearOfBirth;
aliveStatus = "dead"
}
let personObject = { name: people[i].name, age: personAge, status: aliveStatus };
userData.push(personObject);
}
console.log(userData);
let luckyPerson = calculateAge(userData);
console.log(luckyPerson);
}
function calculateAge(userData) {
let oldestPersonName = userData[0].name;
// let oldestPersonAge = userData[0].age;
for (i = 0; i < userData.length; i++) {
if (i === 0) { continue; }
else {
if (userData[i].age > userData[i-1].age ) {
oldestPersonName = userData[i].name;
console.log("current oldies : " + oldestPersonName);
}
}
}
return oldestPersonName;
}
const people = [
{
name: "Carly",
yearOfBirth: 2018
},
{
name: "Ray",
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: "Jane",
yearOfBirth: 1912,
yearOfDeath: 1941
}
];
findTheOldest(people);