-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_nth_fibonacci.js
More file actions
61 lines (48 loc) · 1.4 KB
/
09_nth_fibonacci.js
File metadata and controls
61 lines (48 loc) · 1.4 KB
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
61
/*
Write a function that returns the nth fibonacci term
Examples:
nthFibonacciTerm(1) => 0
nthFibonacciTerm(4) => 2
nthFibonacciTerm(6) => 5
**Your function must return a value**
It's not necessary to print the result on screen,
however to test your function you are free to print the result
*/
function nthFibonacciTerm(number) {
let previousTerm = -1;
let currentTerm = 1;
let nextTerm = 0;
for (let index = 0; index < number; index ++) {
nextTerm = previousTerm + currentTerm;
previousTerm = currentTerm;
currentTerm = nextTerm;
}
return nextTerm;
}
function getMark(isPassed) {
return isPassed ? '✅' : '❌';
}
function getMessage(number, actual, expected) {
let message = "nthFibonacciTerm(" + number + ") should map => '" + expected;
message += "' and is ---> " + actual;
return message;
}
function testNthFibonacciTerm(number, expected) {
const actual = nthFibonacciTerm(number);
const isPassed = actual === expected;
console.log(getMark(isPassed), getMessage(number, actual, expected));
}
function tests() {
testNthFibonacciTerm(0, 0);
testNthFibonacciTerm(1, 0);
testNthFibonacciTerm(2, 1);
testNthFibonacciTerm(3, 1);
testNthFibonacciTerm(4, 2);
testNthFibonacciTerm(5, 3);
testNthFibonacciTerm(6, 5);
testNthFibonacciTerm(7, 8);
testNthFibonacciTerm(8, 13);
testNthFibonacciTerm(9, 21);
testNthFibonacciTerm(10, 34);
}
tests();