-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhowOld.js
22 lines (21 loc) · 864 Bytes
/
howOld.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
Our solution is written as a function expression and uses string interpolation, but it would be equally acceptable to use a function declaration and/or string concatenation
*/
const howOld = (age, year) => {
// The following two lines make it so that our function always knows the current year.
let dateToday = new Date();
let thisYear = dateToday.getFullYear();
// It is totally ok if your function used the current year directly!
// console.log(thisYear);
const yearDifference = year - thisYear;
const newAge = age + yearDifference;
// console.log(newAge);
if (newAge < 0) {
return `The year ${year} was ${-newAge} years before you were born`;
} else if (newAge > age) {
return `You will be ${newAge} in the year ${year}`;
} else {
return `You were ${newAge} in the year ${year}`;
}
};
console.log(howOld(31, 1990));