-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cat years, Dog years.js
54 lines (48 loc) · 1.34 KB
/
Cat years, Dog years.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
// Kata Task
// I have a cat and a dog.
// I got them at the same time as kitten/puppy. That was humanYears years ago.
// Return their respective ages now as [humanYears,catYears,dogYears]
// NOTES:
// humanYears >= 1
// humanYears are whole numbers only
// Cat Years
// 15 cat years for first year
// +9 cat years for second year
// +4 cat years for each year after that
// Dog Years
// 15 dog years for first year
// +9 dog years for second year
// +5 dog years for each year after that
//P: humanYears number
//R:return a array [humanYears,catYears,dogYears]
//E:
//P:
var humanYearsCatYearsDogYears = function(humanYears) {
let catYears=0
let dogYears=0
//for loop le leta hai and use condition lga dega i<humanyears
for(let i=1; i<=humanYears;i++){
if(i==1){
catYears+=15
dogYears+=15
}
else if(i==2){
catYears+=9
dogYears+=9
}
else{
catYears+=4
dogYears+=5
}
}
return [humanYears,catYears,dogYears]
}
console.log(humanYearsCatYearsDogYears(1), [1,15,15])
console.log(humanYearsCatYearsDogYears(2), [2,24,24])
console.log(humanYearsCatYearsDogYears(10), [10,56,64])
//2nd method
var humanYearsCatYearsDogYears = function(y) {
if(y==1) return [1,15,15]
if(y==2) return [2,24,24]
return [y, (y-2) * 4 + 24, (y-2) * 5 + 24]
}