* fix: removed assert msg argument * fix: removed msgs surrounded by 2 single quotes * fix: removed missing 2 assert msg arguments * fix: remove msg surrounded by two single quotes * fix: removed unnecessary assert msg args * fix; remove msgs surrounded by double quotes * fix: removed unnecessary assert msg args * fix: remove unnecessary assert msg args * fix: removed unnecessary assert msg arg * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg arg * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg arg * fix: removed unnecessary assert msg args * fix: Restore expected values to assertions * fix: remove assertion message Co-authored-by: Vivek Agrawal <vivekmittalagrawal@gmail.com>
1.4 KiB
1.4 KiB
title, id, challengeType
title | id | challengeType |
---|---|---|
Fibonacci sequence | 597f24c1dda4e70f53c79c81 | 5 |
Description
nth
Fibonacci number.
The nth
Fibonacci number is given by:
Fn = Fn-1 + Fn-2
The first two terms of the series are 0 and 1.
Hence, the series is: 0, 1, 1, 2, 3, 5, 8, 13...
Instructions
Tests
tests:
- text: <code>fibonacci</code> is a function.
testString: assert(typeof fibonacci === 'function');
- text: <code>fibonacci(2)</code> should return a number.
testString: assert(typeof fibonacci(2) == 'number');
- text: <code>fibonacci(3)</code> should return 1.
testString: assert.equal(fibonacci(3),1);
- text: <code>fibonacci(5)</code> should return 3.
testString: assert.equal(fibonacci(5),3);
- text: <code>fibonacci(10)</code> should return 34.
testString: assert.equal(fibonacci(10),34);
Challenge Seed
function fibonacci(n) {
// Good luck!
}
Solution
function fibonacci(n) {
let a = 0, b = 1, t;
while (--n > 0) {
t = a;
a = b;
b += t;
}
return a;
}