- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Nth element of the Fibonacci series JavaScript
We are required to write a JavaScript function that takes in a single number as the first and the only argument, let’s call that number n.
The function should return the nth element of the Fibonacci series.
For example −
fibonacci(10) should return 55 fibonacci(3) should return 2 fibonacci(6) should return 8 fibonacci(2) should return 1
Example
const fibonacci = (num = 1) => { const series = [1, 1]; for (let i = 2; i < num; i++) { const a = series[i - 1]; const b = series[i - 2]; series.push(a + b); }; return series[num - 1]; }; console.log(fibonacci(10)); console.log(fibonacci(6)); console.log(fibonacci(3)); console.log(fibonacci(2));
Output
And the output in the console will be −
55 8 2 1
- Related Articles
- Python Program for nth multiple of a number in Fibonacci Series
- Java Program for nth multiple of a number in Fibonacci Series
- How to get the nth value of a Fibonacci series using recursion in C#?
- Write a C# function to print nth number in Fibonacci series?
- JavaScript code for recursive Fibonacci series
- Program to find Fibonacci series results up to nth term in Python
- Finding the nth power of array element present at nth index using JavaScript
- Finding nth element of the Padovan sequence using JavaScript
- Generate Fibonacci Series
- Validate a number as Fibonacci series number in JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Fibonacci Series in C#
- Check if number falls in Fibonacci series or not - JavaScript
- Finding nth element of an increasing sequence using JavaScript
- Reduce an array to the sum of every nth element - JavaScript

Advertisements