This repository contains code solutions written in JavaScript for some recursion problems.
Some of the JS files may include functions using the reduce()
method as an alternative solution (practicing!).
I'm adding test files for each problem using Mocha JS test framework (also practicing!) as I upload the solutions in GitHub.
Feel free to solve the problems listed below and compare with my solutions. I would appreciate any feedback or comments from anyone!
# | Problem | Description | Solution | Test |
---|---|---|---|---|
1 | Sum all numbers | Take n and return the sum of all numbers from 1 up to n |
>> | ?? |
2 | Power function | Take base and exp and return the power of a number |
>> | ?? |
3 | Calculate factorial | Take n and return its factorial |
>> | ?? |
4 | Check all values in an array | Take an array and return true if all elements return true in the callback func |
>> | ?? |
5 | Check some values in an array | Take an array and return true if at least 1 element return true in the callback func |
>> | ?? |
6 | Product of an array | Take an array and return the product of all numbers in it |
>> | ?? |
7 | Search a JS object | Take an object and a value and return true if the object contains the value |
>> | ?? |
8 | Count of integers | Take a multi-dimensional array and return the count of integers stored in it |
>> | ?? |
9 | Sum the squares of numbers | Take an array and return the sum of the squares of the numbers in it |
>> | ?? |
10 | Generate repetitions of a number | Take times and n and return an array containing repetitions of n |
>> | ?? |
11 | Recreate the flat() method | Take a multi-dimensional array and return its 1D or flattened version |
>> | ?? |
12 | Fibonacci number position | Take n and return the fibonacci number position |
>> | ?? |
13 | Fibonacci sequence | Take n and return the fibonacci sequence up to n |
>> | ?? |
14 | Sum of fibonacci sequence | Take n and return sum of fibonacci sequence |
>> | ?? |
15 | First n prime numbers from x | Take n (and an optional i ) and return the first n prime numbers from i |
>> | ?? |
16 | Check if brace expression is balanced | Take an expression of braces and return true if it's balanced |
>> | ?? |
Clone this repository
git clone git@github.com:ziaroque/recursion-practice-problems.git
Install npm packages
npm install mocha -D
To test the code, run the command:
npm test
- JS Interview Prep Recursion from codingame.com
- Recursion problems from LeetCode
- some of the problems were provided by @ton03 (ty!)
See the LICENSE file for license rights and limitations (MIT).