Answers for "recursion in javascript"

7

how to make recursive function in javascript

// Basic Recursive function
// It works in all languages but lets try with Javascript

// lets make a greedy factorial calculator

function my_recursive_factorial(nb) // nb to compute his factorial
{
  	if (nb < 0 || nb > 12) // error case
        return 0;
    if (nb == 1 || nb == 0)
        return 1; // you have to check limit for factorial
  
    return nb * my_recursive_factorial(nb - 1); // ! Recursive call !
}

//This will find factorial from 1 to 12 with recursive method

// Recursive functions are greedy and should be used in only special cases who need it
// or who can handle it.

// a function become recursive if she calls herself in stack
Posted by: Guest on August-09-2021
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
1

recursion

/*Java*/
static void recursion(){ recursion(0); }
static void recursion(int x){
	System.out.println("eheh " + x);
    if(x != 666) recursion(x+1);
}
Posted by: Guest on October-28-2020
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 "recursion in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language