# JavaScript Recursion - Exercises, Practice, Solution

## JavaScript Recursion [13 exercises with solution]

[** An editor is available at the bottom of the page to write and execute the scripts.** Go to the editor]

Use recursion to solve the following exercises.

**1. **Write a JavaScript program to calculate the factorial of a number.

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 5! = 5 x 4 x 3 x 2 x 1 = 120

Click me to see the solution

**2. **Write a JavaScript program to find the greatest common divisor (GCD) of two positive numbers using recursion.

Click me to see the solution.

**3. **Write a JavaScript program to get integers in the range (x, y) using recursion.

*Example* : range(2, 9)

*Expected Output :* [3, 4, 5, 6, 7, 8]

Click me to see the solution.

**4. **Write a JavaScript program to compute the sum of an array of integers.

Example : var array = [1, 2, 3, 4, 5, 6]

*Expected Output :* 21

Click me to see the solution.

**5. **Write a JavaScript program to compute the exponent of a number.

Note : The exponent of a number says how many times the base number is used as a factor.

8^{2} = 8 x 8 = 64. Here 8 is the base and 2 is the exponent.

Click me to see the solution.

**6. **Write a JavaScript program to get the first n Fibonacci numbers.

Note : The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . Each subsequent number is the sum of the previous two.

Click me to see the solution.

**7. **Write a JavaScript program to check whether a number is even or not.

Click me to see the solution.

**8. **Write a JavaScript program for binary search.

*Sample array * : [0,1,2,3,4,5,6]

console.log(l.br_search(5)) will return '5'

Click me to see the solution.

**9. **Write a merge sort program in JavaScript.

*Sample array * : [34,7,23,32,5,62]

*Sample output * : [5, 7, 23, 32, 34, 62]

Click me to see the solution.

**10. **Write a JavaScript program to check whether a given string is a palindrome or not using recursion.

A palindrome is a word, number, phrase, or other sequence of symbols that reads the same backwards as forwards, such as the words madam or racecar, the date/time stamps 11/11/11 11:11 and 02/02/2020, and the sentence: "A man, a plan, a canal - Panama".

**Test Data:**

("madam") -> true

("abdb") -> false

("ab") -> false

(test("a") -> true

Click me to see the solution.

**11. **Write a JavaScript program to convert binary number (positive) to decimal number using recursion.

**Test Data:**

(1) -> "1"

(0) -> "0"

(10) -> "1010"

(101) -> "1100101"

Click me to see the solution.

**12. **Write a JavaScript program to search for a given integer in an array of sorted integers using the Binary Search Algorithm and recursion.

**Test Data:**

([1, 2, 3, 5, 6, 7, 10, 11, 14, 15, 17, 19, 20, 22, 23], 6) -> 4

([1, 2, 3, 5, 6, 7, 10, 11, 14, 15, 17, 19, 20, 22, 23], 16) -> -1

Click me to see the solution.

**13. **A string consists of digits ranging from 2 to 9, inclusive. Write a JavaScript program to get all possible letter combinations that represent the number using recursion.

**Test Data:**

("12") -> ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

("9") -> ["y", "z"]

Click me to see the solution.

**More to Come ! **

* To run the code mouse over on Result panel and click on 'RERUN' button.*

**Live Demo: **

See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.

**Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.**

## JavaScript: Tips of the Day

**Reduce method**

[1, 2, 3, 4].reduce((x, y) => console.log(x, y));

The first argument that the reduce method receives is the accumulator, x in this case. The second argument is the current value, y. With the reduce method, we execute a callback function on every element in the array, which could ultimately result in one single value.

In this example, we are not returning any values, we are simply logging the values of the accumulator and the current value.

The value of the accumulator is equal to the previously returned value of the callback function. If you don't pass the optional initialValue argument to the reduce method, the accumulator is equal to the first element on the first call.

On the first call, the accumulator (x) is 1, and the current value (y) is 2. We don't return from the callback function, we log the accumulator and current value: 1 and 2 get logged.

If you don't return a value from a function, it returns undefined. On the next call, the accumulator is undefined, and the current value is 3. undefined and 3 get logged.

On the fourth call, we again don't return from the callback function. The accumulator is again undefined, and the current value is 4. undefined and 4 get logged.

Ref: https://bit.ly/323Y0P6

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook