Check if number falls in Fibonacci series or not - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in a number and checks whether it falls in Fibonacci series or not. We should return a boolean.

Following is the code to check for Fibonacci −

Example

const num = 89;
const isFib = query => {
   if(query === 0 || query === 1){
      return true;
   }
   let prev = 1;
   let count = 2;
   let temp = 0;
   while(count <= query){
      if(prev + count === query){
         return true;
      };
      temp = prev;
      prev = count;
      count += temp;
   };
   return false;
};
console.log(isFib(num));

Output

Following is the output in the console −

true
raja
Published on 16-Sep-2020 09:21:01
Advertisements