Stuck in challenge 110 on JS Basics course

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

// Only change code below this line
var arr = [];
function countdown(n){
 if (n < 1) {
  return [];
} else {
  arr.push(n);
  var counter = countdown(n - 1);
}
if (arr.length === n){
  return arr;
}
}
// Only change code above this line
countdown(20);
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

???


Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
  1. Do not use any global variable here.
  2. Stick to this generic pattern (while learning how to write recursive routines)
function rec( <parameter> ) {
  if ( <it is the end case> ) {
    return <a value>
  } else { //recurse by calling itself
    get reply from rec( <argument> )
    do something to reply and 
    return this result to the caller
  }
}

Following the pattern, here’s a recursive function to compute the sum of an array

function sum(numbers) {
   if (numbers.length === 0) {
     return 0;
  } else {
     //recursive call. Note we're passing an array minus 
    // the first element
    const reply = sum(numbers.slice(1)); //reply is sum of numbers[1]...last element
    const result = reply + numbers[0]; //add the first element 
    return result
  }
}

The countdown function is kind of reverse of sum because now you’re passing an integer and getting an array back. You have correct parts in your code. Following the pattern, the code will be like

function countdown(n) {
  if (n < 1) {
    return [];
  } else {
    const reply = countdown(n - 1); 
       // what is a 'reply' a number? an array?
       // what are you returning from the end case?
      // using 'reply' get the 'result' to return to the caller
     //Follow the generic pattern
  }
}

i agree about the global variable but when i leave the variable inside the function
it initiate itself every loop.
so how can i solve this?

You need to use the return value from the recursive call rather than discarding it.

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1); // RIGHT HERE THE RETURN VALUE IS STORED
    countArray.push(n); // RIGHT HERE THE RETURN VALUE IS USED
    return countArray; // RIGHT HERE THE MODIFIED RETURN VALUE IS RETURNED FROM THIS FUNCTION CALL
  }
}
console.log(countup(5));

Thanks!
how can n be pushed to a function?
why the return function doesn’t break the loop?

There is no loop. Recursion is not the same thing as a loop. Recursion uses several separate function calls. N is not being pushed to a function. N is being pushed onto the array that was returned by the function call.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.