1.6 KiB
1.6 KiB
title | id | challengeType |
---|---|---|
Fibonacci sequence | 597f24c1dda4e70f53c79c81 | 5 |
Description
Instructions
Tests
tests:
- text: <code>fibonacci</code> is a function.
testString: assert(typeof fibonacci === 'function', '<code>fibonacci</code> is a function.');
- text: <code>fibonacci(2)</code> should return a number.
testString: assert(typeof fibonacci(2) == 'number', '<code>fibonacci(2)</code> should return a number.');
- text: <code>fibonacci(3)</code> should return 1.
testString: assert.equal(fibonacci(3),1,"<code>fibonacci(3)</code> should return 1.");
- text: <code>fibonacci(5)</code> should return 3.
testString: assert.equal(fibonacci(5),3,"<code>fibonacci(5)</code> should return 3.");
- text: <code>fibonacci(10)</code> should return 34.
testString: assert.equal(fibonacci(10),34,"<code>fibonacci(10)</code> should return 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;
}