Answers for "recursion js"

1

javascript recursion over array

function 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);
}
Posted by: Guest on February-08-2020
0

recursion js

// program to count down numbers to 1
function countDown(number) {

    // display the number
    console.log(number);

    // decrease the number value
    const newNumber = number - 1;

    // base case
    if (newNumber > 0) {
        countDown(newNumber);
    }
}

countDown(4);
Posted by: Guest on September-12-2021
0

recursion in javascript

var countdown = function(value) {
    if (value > 0) {
        console.log(value);
        return countdown(value - 1);
    } else {
        return value;
    }
};
countdown(10);
Posted by: Guest on January-05-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language