- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Validate a number as Fibonacci series number in JavaScript
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 on this basis.
Example
The code for this will be −
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 on console −
true
Advertisements