
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Fibonacci like sequence in JavaScript
Let us define a sequence using the following definition −
Given terms t1 and t2, the two consecutive terms of this sequence, then the third term of this sequence will equal to −
t3 = t1 + (t2 * t2)
Like the Fibonacci sequence, the first two terms of this sequence will always be 0 and 1 respectively.
We are required to write a JavaScript function that takes in a number, say n. The function should then compute and return the nth term of the sequence described above.
For example − If n = 6, then
t6 = 27
because the sequence is −
0 1 1 2 5 27
Example
The code for this will be −
const num = 6; const findSequenceTerm = (num = 1) => { const arr = [0, 1]; while(num > arr.length){ const last = arr[arr.length − 1]; const secondLast = arr[arr.length − 2]; arr.push(secondLast + (last * last)); }; return arr[num − 1]; }; console.log(findSequenceTerm(num));
Output
And the output in the console will be −
27
- Related Articles
- The Fibonacci sequence in Javascript
- Finding Fibonacci sequence in an array using JavaScript
- Check if the n-th term is odd or even in a Fibonacci like sequence
- 8085 program to generate Fibonacci sequence
- 8086 program to generate Fibonacci Sequence
- How to print the Fibonacci Sequence using Python?
- Python Program to Display Fibonacci Sequence Using Recursion
- Checking for Fibonacci numbers in JavaScript
- Sum of even Fibonacci terms in JavaScript
- JavaScript code for recursive Fibonacci series
- Nth element of the Fibonacci series JavaScript
- Validate a number as Fibonacci series number in JavaScript
- Strictly increasing sequence JavaScript
- Validating push pop sequence in JavaScript
- Check if number falls in Fibonacci series or not - JavaScript

Advertisements