-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_7.js
36 lines (31 loc) · 856 Bytes
/
exercise_7.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
// timeLeft = 66-15(51) count+1 makanTerusRekursif(timeLeft) =>
// timeLeft = 51-15(46) count+1 makanTerusRekursif(timeLeft) =>
function makanTerusRekursif(waktu) {
var timeCount;
var timeLeft;
var count;
if ( (typeof waktu === 'object') ){ // [timeLeft, count]
timeCount = waktu;
timeLeft = timeCount[0];
count = timeCount[1];
} else{ // still not in obj form
count = 0;
timeLeft = waktu;
}
// base
if (timeLeft <= 0){
// console.log(count);
return count;
}
// recursive
timeLeft -= 15;
count += 1;
timeCount = [timeLeft, count];
return makanTerusRekursif(timeCount);
}
// TEST CASES
console.log(makanTerusRekursif(66)); // 5
console.log(makanTerusRekursif(100)); // 7
console.log(makanTerusRekursif(90)); // 6
console.log(makanTerusRekursif(10)); // 1
console.log(makanTerusRekursif(0)); // 0