Skip to content

Instantly share code, notes, and snippets.

@gauravds
Last active October 3, 2025 08:59
Show Gist options
  • Select an option

  • Save gauravds/26731db1989223debd53deedef38ea2d to your computer and use it in GitHub Desktop.

Select an option

Save gauravds/26731db1989223debd53deedef38ea2d to your computer and use it in GitHub Desktop.
findFibIndex
/**
* Assignment: Find Fibonacci Index
*
* Instructions:
* 1. Implement the function `findFibIndex(n)` that returns the 1-based index of
* the number `n` in the Fibonacci sequence.
* 2. If the number does not exist in the sequence, return -1.
* 3. The Fibonacci sequence starts with: 0, 1, 1, 2, 3, 5, 8, ...
* 4. Do not use recursion — use iterative logic instead.
* 5. Write and run console tests to verify your solution.
*
* Examples:
* findFibIndex(5) → 6
* findFibIndex(4) → -1
* findFibIndex(21) → 9
* findFibIndex(0) → 1
* findFibIndex(-5) → -1
*/
function findFibIndex(n) {
return -1
}
/** Console tests (value, isExpected) */
console.log(findFibIndex(5), findFibIndex(5) === 6); // 6 true
console.log(findFibIndex(4), findFibIndex(4) === -1); // -1 true
console.log(findFibIndex(21), findFibIndex(21) === 9); // 9 true
console.log(findFibIndex(0), findFibIndex(0) === 1); // 1 true
console.log(findFibIndex(1), findFibIndex(1) === 2); // 2 true
console.log(findFibIndex(144),findFibIndex(144)=== 13); // 13 true
console.log(findFibIndex(-5), findFibIndex(-5) === -1); // -1 true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment