AlgoDaily Solution

1var assert = require('assert');
2
3/*
4 * @param {number} n The Fibonacci number to get.
5 * @returns {number} The nth Fibonacci number
6 */
7
8function fibonacci(num) {
9  if (num < 0) throw 'num must be >= 0';
10  if (num === 0) return 0;
11  if (num === 1) return 1;
12
13  return fibonacci(num - 1) + fibonacci(num - 2);
14}
15
16try {
17  assert.equal(fibonacci(1), 1);
18
19  console.log('PASSED: ' + '`fibonacci(1)` should return `1`');
20} catch (err) {
21  console.log(err);
22}
23
24try {
25  assert.equal(fibonacci(17), 1597);
26
27  console.log('PASSED: ' + '`fibonacci(17)` should return `1597`');
28} catch (err) {
29  console.log(err);
30}
31
32try {
33  assert.equal(fibonacci(34), 5702887);
34
35  console.log('PASSED: ' + '`fibonacci(34)` should return `5702887`');
36} catch (err) {
37  console.log(err);
38}

Community Solutions

Community solutions are only available for premium users.

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.

JAVASCRIPT
OUTPUT
Results will appear here.