Tell us what’s happening:
Hi, I’m having trouble understanding why this doesn’t work, and I would really appreciate any help.
I tested the code in codepen.io and it works fine if I pass an empty array and any number as an argument, if I call the function like this:
countdown([], 5);
but if I call it using the name of the variable myArray, it doesn’t work, unless it is previously defined, like:
//This doesn’t work:
countdown(myArray, 5);
//This works:
var MyArray = [];
countdown(myArray, 5);
So I’m not sure if the variable myArray is supposed to be already defined in the example or not, especially because when I tested the proposed solution in codepen, it doesn’t work…
This is the proposed solution:
function countdown(myArray, n){ if(n <= 0){ return; } else{ myArray.push(n); countdown(myArray, n - 1); } }
This is my code so far:
//Only change code below this line
function countdown(myArray, n) {
if (n < 1) {
return [];
} else {
var myArray = countdown(myArray, n -1);
myArray.unshift(n);
return myArray;
}
}
Thanks in advance!!
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0
.
Challenge: Use Recursion to Create a Countdown
Link to the challenge: