• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

264
Views
How to write a function generating Fibonacci series smaller than a given number (javascript)?

Here's my code:

function fibs(num) {
  //generate Fibonacci numbers:
  let arr = [1,1]
  let i = 2;
  function fibsRange(i) {
    arr[i] = arr[i-1] + arr[i-2]
    if (arr[i] < num) {
      fibsRange(i+1);//call function one more time;
    }
      return arr
  }
  return fibsRange();      
}
console.log(fibs(5)); 

Assume that given number (num) is bigger than 2. Where do I get wrong?

Note: I edited my code.

almost 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

first :as mentionned by Phil your function (the first one) needs to return something. second: you don't need 2 functions, one will do the job. here's a working version of your code (I also modified some unnecessary code.

Edit: I added the case when you need to parse just one parameter

function fibsRange(i,arr,num) {
  if(arr[i-1]+arr[i-2] > num) return arr
  else{
    arr[i] = arr[i - 1] + arr[i - 2]
    return fibsRange(i + 1,arr,num);
  }
}
//in case you need just one parameter
function fib(num){
  let arr = [1, 1];
  let i = 2;
  return fibsRange(i,arr,num);
}

//those(declaration of arr and i) are unnecessary in case you use fib
let arr = [1, 1]
let i = 2;
console.log(fibsRange(i,arr,5));
console.log(fib(5));

almost 3 years ago · Juan Pablo Isaza Report

0

You never called your fibsRange() function and never returned anything from fibs().

The following version works.

function fibs(num) {
  //generate Fibonacci numbers:
  let arr = [1, 1]
  if (num>1) fibsRange(2); 

  function fibsRange(i) {
    arr[i] = arr[i - 1] + arr[i - 2]
    if (arr[i] < num) {
      fibsRange(i + 1); //call function one more time;
    }       
  }
  return arr;
}
console.log(fibs(5)); //undified;

almost 3 years ago · Juan Pablo Isaza Report

0

Your code is checking if arr[i] < num, then invoke the fibsRange(i+1). This is a wrong logic, you will always get the fibbonacci number just higher than the num because you are already setting it at arr[i] and then checking the condition. However, you should first check if arr[i] < num, and then push it into arr.

function fibs(num) {
  //generate Fibonacci numbers:
  let arr = [1, 1];
  let i = 2;
  function fibsRange(i) {
    const lastFibNumber = arr[i - 1] + arr[i - 2];
    if (lastFibNumber < num) {
      arr.push(lastFibNumber);
      fibsRange(i + 1); //call function one more time;
    }
    return arr;
  }
  return fibsRange(i);
}

console.log(fibs(5));
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error