Recursion in javascript (non-informative content)
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Recursion
An act of a function calling itself. Recursion is used to solve problems that contain smaller sub-problems. A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (continues recursion). (MDN)
Use cases
Sum function with traditional for loop
1
2
3
4
5
6
7
8
9
10
11
function sum(arr) {
let result = arr[0];
for (let i = 1; i < arr.length; i++) {
result = result + arr[i];
}
return result;
}
console.log(`Array: [1,2,3]; Expected: 6; Actual: ${sum([1,2,3])}`);
console.log(`Array: [1,0,1]; Expected: 2; Actual: ${sum([1,0,1])}`);
Enter to Rename, Shift+Enter to Preview
Sum function with recursion
Deatiled example:
1
2
3
4
5
6
7
8
9
10
function sum(arr, initial = 0) {
if (arr.length === 0) {
return initial;
}
return sum(arr.slice(1), initial + arr[0]);
}
console.log(`Array: [1,2,3]; Expected: 6; Actual: ${sum([1,2,3])}`);
console.log(`Array: [1,0,1]; Expected: 2; Actual: ${sum([1,0,1])}`);
Enter to Rename, Shift+Enter to Preview
Deep map function without recursion
1
2
3
4
5
6
7
8
9
10
11
12
13
function deepMapAtoB(arr) {
return arr.map(value => {
if (Array.isArray(value)) {
return value.map(nestedValue => {
return nestedValue === 'a' ? 'b' : nestedValue;
});
}
return value === 'a' ? 'b' : value;
});
}
console.log(`Given: ["a",["a"],"c"]; Result: ${JSON.stringify(deepMapAtoB(["a",["a"],"c"]))}`);
Enter to Rename, Shift+Enter to Preview
Deep map function with recursion
1
2
3
4
5
6
7
8
9
10
11
function deepMapAtoB(arr) {
return arr.map(value => {
if (Array.isArray(value)) {
return deepMapAtoB(value);
}
return value === 'a' ? 'b' : value;
});
}
console.log(`Given: ["a",["a"],"c"]; Result: ${JSON.stringify(deepMapAtoB(["a",["a"],"c"]))}`);
Enter to Rename, Shift+Enter to Preview
Translated resources
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content