
Practice 50 JavaScript Recursion Programming Questions
Q1. Write a JavaScript program to find the factorial of a number using recursion.
Input: factorial(5)
Expected Output: 120
Q2. Write a JavaScript program to calculate the nth Fibonacci number using recursion.
Input: fibonacci(6)
Expected Output: 8
Q3. Write a JavaScript program to calculate the sum of all elements in an array using recursion.
Input: sumArray([1, 2, 3, 4])
Expected Output: 10
Q4. Write a JavaScript program to reverse a string using recursion.
Input: reverseString("hello")
Expected Output: "olleh"
Q5. Write a JavaScript program to calculate the power of a number using recursion.
Input: power(2, 3)
Expected Output: 8
Q6. Write a JavaScript program to find the sum of digits of a number using recursion.
Input: sumDigits(123)
Expected Output: 6
Q7. Write a JavaScript program to check if a string is a palindrome using recursion.
Input: isPalindrome("racecar")
Expected Output: true
Q8. Write a JavaScript program to calculate the greatest common divisor (GCD) of two numbers using recursion.
Input: gcd(48, 18)
Expected Output: 6
Q9. Write a JavaScript program to count the number of vowels in a string using recursion.
Input: countVowels("hello")
Expected Output: 2
Q10. Write a JavaScript program to flatten a nested array using recursion.
Input: flatten([1, [2, [3, 4]]])
Expected Output: [1, 2, 3, 4]
Q11. Write a JavaScript program to calculate the length of a string using recursion.
Input: stringLength("hello")
Expected Output: 5
Q12. Write a JavaScript program to check if an array is sorted using recursion.
Input: isSorted([1, 2, 3, 4])
Expected Output: true
Q13. Write a JavaScript program to count the occurrences of a character in a string using recursion.
Input: countChar("hello", "l")
Expected Output: 2
Q14. Write a JavaScript program to calculate the product of an array of numbers using recursion.
Input: productArray([1, 2, 3, 4])
Expected Output: 24
Q15. Write a JavaScript program to calculate the sum of even numbers in an array using recursion.
Input: sumEven([1, 2, 3, 4])
Expected Output: 6
Q16. Write a JavaScript program to convert a decimal number to binary using recursion.
Input: decimalToBinary(10)
Expected Output: "1010"
Q17. Write a JavaScript program to count the number of elements in a nested array using recursion.
Input: countElements([1, [2, [3, 4]]])
Expected Output: 4
Q18. Write a JavaScript program to find the minimum element in an array using recursion.
Input: findMin([3, 5, 1, 4])
Expected Output: 1
Q19. Write a JavaScript program to calculate the sum of a geometric series using recursion.
Input: geometricSum(3, 2)
Expected Output: 7 (1 + 2 + 4)
Q20. Write a JavaScript program to replace all occurrences of a character in a string using recursion.
Input: replaceChar("hello", "l", "x")
Expected Output: "hexxo"
Q21. Write a JavaScript program to find the nth triangular number using recursion.
Input: triangularNumber(5)
Expected Output: 15
Q22. Write a JavaScript program to calculate the harmonic sum of n using recursion.
Input: harmonicSum(3)
Expected Output: 1.833 (1 + 1/2 + 1/3)
Q23. Write a JavaScript program to count the number of zeros in a number using recursion.
Input: countZeros(10204)
Expected Output: 2
Q24. Write a JavaScript program to find the depth of a nested array using recursion.
Input: arrayDepth([1, [2, [3, 4]]])
Expected Output: 3
Q25. Write a JavaScript program to calculate the sum of odd numbers in an array using recursion.
Input: sumOdd([1, 2, 3, 4, 5])
Expected Output: 9
Q26. Write a JavaScript program to remove duplicates from an array using recursion.
Input: removeDuplicates([1, 2, 2, 3, 3, 4])
Expected Output: [1, 2, 3, 4]
Q27. Write a JavaScript program to calculate the sum of squares of numbers in an array using recursion.
Input: sumOfSquares([1, 2, 3])
Expected Output: 14
Q28. Write a JavaScript program to find the first uppercase letter in a string using recursion.
Input: firstUppercase("helloWorld")
Expected Output: "W"
Q29. Write a JavaScript program to calculate the sum of numbers from 1 to n using recursion.
Input: sumToN(10)
Expected Output: 55
Q30. Write a JavaScript program to remove all non-alphanumeric characters from a string using recursion.
Input: cleanString("he!!llo@")
Expected Output: "hello"
Q31. Write a JavaScript program to check if a number is a power of two using recursion.
Input: isPowerOfTwo(16)
Expected Output: true
Input: isPowerOfTwo(18)
Expected Output: false
Q32. Write a JavaScript program to calculate the nth Lucas number using recursion.
Input: lucasNumber(5)
Expected Output: 11
Q33. Write a JavaScript program to reverse an array using recursion.
Input: reverseArray([1, 2, 3, 4])
Expected Output: [4, 3, 2, 1]
Q34. Write a JavaScript program to calculate the binomial coefficient (nCr) using recursion.
Input: binomialCoefficient(5, 2)
Expected Output: 10
Q35. Write a JavaScript program to check if a number is a palindrome using recursion.
Input: isNumberPalindrome(121)
Expected Output: true
Input: isNumberPalindrome(123)
Expected Output: false
Q36. Write a JavaScript program to calculate the product of two numbers without using multiplication, using recursion.
Input: multiply(3, 4)
Expected Output: 12
Q37. Write a JavaScript program to find the nth Catalan number using recursion.
Input: catalanNumber(3)
Expected Output: 5
Q38. Write a JavaScript program to find the maximum depth of a binary tree represented as a nested array using recursion.
Input: maxDepth([1, [2, [3]]])
Expected Output: 3
Q39. Write a JavaScript program to calculate the sum of an arithmetic series using recursion.
Input: arithmeticSum(3, 5)
Expected Output: 15 (3 + 4 + 5)
Q40. Write a JavaScript program to count the number of uppercase letters in a string using recursion.
Input: countUppercase("HeLLo")
Expected Output: 3
Q41. Write a JavaScript program to calculate the product of digits in a number using recursion.
Input: productOfDigits(234)
Expected Output: 24
Q42. Write a JavaScript program to calculate the length of a nested array using recursion.
Input: nestedArrayLength([1, [2, [3, 4]], 5])
Expected Output: 5
Q43. Write a JavaScript program to find the sum of natural numbers up to n that are divisible by 3 or 5 using recursion.
Input: sumDivisibleByThreeOrFive(10)
Expected Output: 33
Q44. Write a JavaScript program to calculate the LCM (least common multiple) of two numbers using recursion.
Input: lcm(12, 15)
Expected Output: 60
Q45. Write a JavaScript program to find the index of the first occurrence of a target in an array using recursion.
Input: findIndex([1, 2, 3, 4, 5], 3)
Expected Output: 2
Q46. Write a JavaScript program to calculate the nth tetrahedral number using recursion.
Input: tetrahedralNumber(4)
Expected Output: 10
Q47. Write a JavaScript program to find the number of consonants in a string using recursion.
Input: countConsonants("hello world")
Expected Output: 7
Q48. Write a JavaScript program to implement recursive binary search in an array.
Input: binarySearch([1, 2, 3, 4, 5], 3)
Expected Output: 2
Q49. Write a JavaScript program to split a string into an array of words using recursion.
Input: splitWords("hello world")
Expected Output: ["hello", "world"]
Q50. Write a JavaScript program to calculate the digital root of a number using recursion.
Input: digitalRoot(9875)
Expected Output: 2