done find the oldest exercise
This commit is contained in:
		
							parent
							
								
									bccd35b40d
								
							
						
					
					
						commit
						9774d20102
					
				|  | @ -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 | ||||
|  |  | |||
|  | @ -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', | ||||
|  |  | |||
|  | @ -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> | ||||
|  | @ -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); | ||||
		Loading…
	
		Reference in New Issue