Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69306364df283fcaff2e1ad6.md
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69306364df283fcaff2e1ad6 Challenge 145: Nth Fibonacci Number 28 challenge-145

--description--

Given an integer n, return the nth number in the fibonacci sequence.

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The first 10 numbers in the sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

--hints--

nthFibonacci(4) should return 2.

assert.equal(nthFibonacci(4), 2);

nthFibonacci(10) should return 34.

assert.equal(nthFibonacci(10), 34);

nthFibonacci(15) should return 377.

assert.equal(nthFibonacci(15), 377);

nthFibonacci(40) should return 63245986.

assert.equal(nthFibonacci(40), 63245986);

nthFibonacci(75) should return 1304969544928657.

assert.equal(nthFibonacci(75), 1304969544928657);

--seed--

--seed-contents--

function nthFibonacci(n) {

  return n;
}

--solutions--

function nthFibonacci(n) {
  if (n === 1) return 0;
  if (n === 2) return 1;

  let a = 0;
  let b = 1;

  for (let i = 3; i <= n; i++) {
    const next = a + b;
    a = b;
    b = next;
  }

  return b;
}