-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhowOld.js
60 lines (39 loc) · 1.98 KB
/
howOld.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Write your function here:
// get current year
const today = new Date();
const thisYear = today.getFullYear();
function howOld(age, year) {
// determine born year current year - age
let currentYear = thisYear;
let bornYear = currentYear - age;
if (year > currentYear){
return `You will be ${year - bornYear} in the year ${year}`;
} else if (year < bornYear) {
return `The year ${year} was ${bornYear - year} years before you were born`;
} else if (year < currentYear && year > bornYear) {
return `You were ${year - bornYear} in the year ${year}`;
}
}
console.log(howOld(45, 1978));
// console.log(thisYear)
// Once your function is written, write function calls to test your code!
// ============================================================================
/* INSTRUCTIONS FROM CODECADEMY */
/*
Write a function, howOld(), that has two number parameters, age and year,
and returns how old someone who is currently that age was (or will be) during that year.
Handle three different cases:
* If the year is in the future, you should return a string in the following format:
'You will be [calculated age] in the year [year passed in]'
* If the year is before they were born, you should return a string in the following format:
'The year [year passed in] was [calculated number of years] years before you were born'
* If the year is in the past but not before the person was born,
you should return a string in the following format:
'You were [calculated age] in the year [year passed in]'
Hint
You might find these two variables helpful:
const yearDifference = year - theCurrentYear
const newAge = age + yearDifference
Once you have newAge, you’ll be able to handle the three difference cases.
If the newAge is less than 0, this means the year provided was before the person was born. If the newAge is greater than their current age, this means the year passed in is in the future. Otherwise, we know the year provided was in the past but not before they were born.
*/