Skip to content

Commit fc8231b

Browse files
recursion programs added and readme updated
1 parent c7f91eb commit fc8231b

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ When we have **String** or **Array** which creating a WINDOW from one position t
4141
### Divide and Conquer Pattern
4242

4343
It is related to our search and sorting algorithms, such as in Binery Search we divide the array from mid then search the element.
44+
45+
### Recursion
46+
47+
Keeps calling same function until didn't get expected result. Here few Points are so important otherwise our Call Stack will be infinite and size exceeding error comes:
48+
49+
- Identify base case such as if (statement) return x;
50+
- It basically depends upon call stack, you can check call stack example in Examples directory.

Recursion/arithmetic-operations.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
console.log('~~~~~~~ Sum ~~~~~~~~');
2+
3+
const sumRange = (uptoNum) => {
4+
console.log('uptoNum',uptoNum);
5+
if(uptoNum === 1) return 1;
6+
return uptoNum + sumRange(uptoNum -1);
7+
}
8+
9+
console.log(sumRange(2));
10+
console.log('~~~~~~~~~~~~~~~~~~~~~')
11+
console.log(sumRange(3));
12+
console.log('~~~~~~~~~~~~~~~~~~~~~')
13+
console.log(sumRange(4));
14+
console.log('~~~~~~~~~~~~~~~~~~~~~')
15+
console.log(sumRange(5));
16+
17+
console.log('~~~~~~~~~~~~~~~~~~~~~')
18+
console.log('~~~~~~~ Factorial ~~~~~~~~');
19+
console.log('~~~~~~~~~~~~~~~~~~~~~')
20+
21+
const factorial = (uptoNum) => {
22+
console.log('uptoNum',uptoNum);
23+
if(uptoNum === 1) return 1;
24+
return uptoNum * factorial(uptoNum -1);
25+
}
26+
27+
console.log(factorial(2));
28+
console.log('~~~~~~~~~~~~~~~~~~~~~')
29+
console.log(factorial(3));
30+
console.log('~~~~~~~~~~~~~~~~~~~~~')
31+
console.log(factorial(4));
32+
console.log('~~~~~~~~~~~~~~~~~~~~~')
33+
console.log(factorial(5));

Recursion/multidimensional-operations.js

Whitespace-only changes.

Recursion/recursions.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Write a function called power which accepts a base and an exponent.
2+
// The function should return the power of the base to the exponent.
3+
// This function should mimic the functionality of Math.pow() - do not worry about negative bases and exponents.
4+
console.log(`------- Question:1 --------`);
5+
const power = (base, exp) => {
6+
console.log(base, exp)
7+
if(exp === 0) return 1;
8+
if(exp === 1) return base * 1;
9+
return base * power(base, exp - 1);
10+
}
11+
12+
console.log(power(2,0)) // 1
13+
console.log('----------------')
14+
console.log(power(2,2)) // 4
15+
console.log('----------------')
16+
console.log(power(2,4)) // 16
17+
console.log('----------------')
18+
19+
// Write a function called productOfArray which takes in an array of numbers and returns the product of them all.
20+
console.log(`------- Question:2 --------`);
21+
const productOfArray = (inputArr) => {
22+
let result = 1;
23+
for(let i=0;i<inputArr.length; i++) {
24+
result *= inputArr[i]
25+
}
26+
return result;
27+
}
28+
29+
console.log(productOfArray([1,2,3])) // 6
30+
console.log(productOfArray([1,2,3,10])) // 60

0 commit comments

Comments
 (0)