Skip to content

Latest commit

 

History

History
35 lines (24 loc) · 1.62 KB

File metadata and controls

35 lines (24 loc) · 1.62 KB

JavaScript algorithms to practice - Recursion

Practice algorithm questions focused on recursion - found on w3r.

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

  2. Write a JavaScript program to find the greatest common divisor (gcd) of two positive numbers.

  3. Write a JavaScript program to get the integers in range (x, y). Example : range(2, 9) Expected Output : [3, 4, 5, 6, 7, 8]

  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

  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. 82 = 8 x 8 = 64. Here 8 is the base and 2 is the exponent.

  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.

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

  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'

  9. Write a merge sort program in JavaScript. Sample array : [34,7,23,32,5,62] Sample output : [5, 7, 23, 32, 34, 62]