Use Recursion to Create a Countdown

Tell us what’s happening:

Tell me please, what is wrong with my code

function countdown(myArray, n){

if (n == -1) {

return [];

} else if (n > 1) {

myArray = countdown(myArray, n-1);

myArray.unshift(n);

return myArray;

}

}

mistake - ReferenceError: Can’t find variable: myArray
Your code so far


//Only change code below this line
function countdown(myArray, n){
if (n == -1) {
 return [];
} else if (n > 1) {
 myArray = countdown(myArray, n-1);
 myArray.unshift(n);
return myArray;
}
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.1 Safari/605.1.15.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown

Step through an execution of your code - what happens when n == 1? What does it return? How does that return value affect the rest of the stack as your recursion unwinds?

@Anastasiya With the code you have posted above, I do not see that error on the page or in the browser console? Can you post a screenshot?

This is the problem:

 myArray = countdown(myArray, n-1);

You modified the parameter reference (myArray), which it’s not good at all. In fact, if you comment this line, you won’t get the error. If you want to do it in that way, declare a local variable. This happen because JS have call by sharing. It’s a good practice don’t reassign function parameter inside functions. If you do, the parameter doesn’t reference the original object anymore.

1 Like

Wrong @. It’s not my code!

edit: @ was fixed. I also don’t get the error referenced by the original poster, but the logic error I tried to lead them toward is still a problem.

@partylich Sorry about that. I corrected the @.

1 Like

I tested the code provided by the user (shown above), but I do not see the undefined error message. Where are you all seeing the message and what browser are you using? There may be a bug in the tests. The code above does not solve the challenge, but I find it interesting that I do not see the error message on Chrome.

The error shows in Chrome and Firefox. If you look at the console in Chrome you will see “TypeError: Cannot read property ‘unshift’ of undefined”. It’s not a browser or test error, @Anastasiya is modifying myArray reference inside the function, that the problem.

1 Like

As noted earlier, if you run OPs function with n = 1, it does not meet any of the conditionals and thus returns undefined. You can alter her code while leaving in the myArray mutation and still pass the tests. Immutability is not one of the stated or tested requirements. I can understand emphasizing it early in one’s learning, but it’s not the actual problem that causes the tests to fail in this case.

2 Likes

Right, I didn’t realized that. My fault… :raised_hand:

Thanks everyone!
I rewrote the function and it worked. Now it looks like this.

function countdown(myArray, n){
  if (n <= 0) {
      return;
      } else {
        myArray.push(n);  
        countdown(myArray, n - 1);
        }
        };
2 Likes