recursive function javascript
// Count Up
function countup(n) {
//Base case
if (n < 1) {
return [];
} else {
// The Recursion
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
// Count Down
function countdown(n) {
// Base case
if (n < 1) {
return [];
} else {
// The Recursion
const count = countdown(n - 1);
count.unshift(n);
return count;
}
}
// User Defined
function rangeOfNumbers(startNum, endNum) {
// Base case
if (startNum - endNum === 0) {
return [startNum];
} else {
// The Recursion
const count = rangeOfNumbers(startNum, endNum - 1);
count.push(endNum);
return count;
}
}function pow(x, n) {
if (n == 1) {
return x;
} else {
return x * pow(x, n - 1);
}
}
alert( pow(3, 3) ); // 27function recurse(arr=[])
{
// base case, to break the recursion when the array is empty
if (arr.length === 0) {
return;
}
// destructure the array
const [x, ...y] = arr;
// Do something to x
return recurse(y);
}
Also in JavaScript:
- jquery scroll to id
- check object has key javascript
- can we get the value of form control after disabling it angular
- js test if string
- webpack react proxy not working
- javascript reverse each string element in array
- react enzyme
- web worker multiple data
- nextjs svg npm
- generate random int js
- javascript Convert an array of strings to numbers
- straforma in stringa js
- Aspect Ratio height react
- jquery animation
- array methods in javascript
- javascript for loop starting from end
- w3 js resize event
- how to split two digit number in javascript
- how to display data from json api using flutter expansiontile
- get top items from json object
- javascript document object model getElementsByClassName
- print in javascript
- select class with data attribute jquery
- react social login buttons