
- 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
Finding the nth element of the lucas number sequence in JavaScript
Lucas Numbers
Lucas numbers are numbers in a sequence defined like this −
L(0) = 2 L(1) = 1 L(n) = L(n-1) + L(n-2)
Problem
We are required to write a JavaScript function that takes in a number n and return the nth lucas number.
Example
Following is the code −
const num = 21; const lucas = (num = 1) => { if (num === 0) return 2; if (num === 1) return 1; return lucas(num - 1) + lucas(num - 2); }; console.log(lucas(num));
Output
Following is the console output −
24476
- Related Articles
- Finding nth element of the Padovan sequence using JavaScript
- Finding nth element of an increasing sequence using JavaScript
- Finding nth digit of natural numbers sequence in JavaScript
- Finding the nth prime number in JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding the nth palindrome number amongst whole numbers in JavaScript
- Finding the nth missing number from an array JavaScript
- Finding the missing number in an arithmetic progression sequence in JavaScript
- Finding the nth digit of natural numbers JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Finding the longest "uncommon" sequence in JavaScript
- Finding number of occurrences of the element appearing most number of times in JavaScript
- Nth element of the Fibonacci series JavaScript
- Finding one missing number in a scrambled sequence using JavaScript

Advertisements