Tell us what’s happening:
Hello there, this is my first post.
The test says I’m wrong, whereas I’m pretty sure that i’m being right, i already tested it in another editor and it work. So i don’t know where is the probleme, maybe it has something to do with the way it’s tested.
Your code so far
//Only change code below this line
function countdown(myArray, n){
if (n == 1) {
return [1];
} else if (n > 1) {
myArray = countdown(myArray, n-1);
myArray.unshift(n);
return myArray;
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0.
function countdown(myArray, n){
if (n >= 1) {
myArray = countdown(myArray, n-1);
myArray.unshift(n);
}
return myArray;
}
If you want to leave your code as is, then what is messing up the test is the fact that you are just returning [1] as apposed to shifting it into your myArray variable.
you have myArray as function parameter, so a certain value is passed in as function argument, but then you make a new variable with the same name that is an empty array essentially overwriting previous variable
which defines a myArray variable outside of the function in the global scope and is available to the rest of your program. Or, if you don’t plan to save the results, you can simply use:
function countdown(myArray, n){
...
};
countdown([], 5);
I copy/pasted your code into the Chrome console and got the following error:
Uncaught ReferenceError: Cannot access 'myArray' before initialization
at countdown (<anonymous>:5:31)
at <anonymous>:1:1
I then changed the last three lines as follows:
const numbers = countdown(myArray, n - 1);
numbers.unshift(n);
return numbers;
In your line, const myArray = countdown(myArray, n - 1); you are attempting to define a block-level variable named myArray, but you are also referencing myArray in the expression, countdown(myArray, n - 1). Now, you and I know you meant the parameter myArray that was passed into the function, but the interpreter saw it as referencing the new variable you were creating.
The best thing is to use a different name for the new array. I chose the name numbers, but any name but myArray (or n) would do.
Hey this was the solution and it did not pass tests 2 and 3:
function countdown(myArray, n) {
if (n <= 0) {
return [];
} else {
const numbers = countdown(myArray, n - 1);
numbers.unshift(n);
return numbers;
}
}
This is the solution that FCC lists:
function countdown(myArray, n) {
if (n <= 0) {
return;
} else {
myArray.push(n);
countdown(myArray, n - 1);
}
}
when i run either of these in the VS Code/Chrome console with console.log(countdown(myArray, 10)); i get the error ReferenceError: myArray is not defined but if i call the function with console.log(countdown(Array, 10)); my original solution works as intended but FCC’s solution then gives me another error TypeError: myArray.push is not a function.
Why is my solution wrong on FCC and FCC’s solution is wrong on VS Code/Chrome console? and why cant I console log the test case FCC lists countdown(myArray, 10) for either of these solutions on VSCode/Chrome console without an error?
myArray is local to the countdown function, so if you try to call (from the global scope) the function with a variable named myArray as the first argument, you will get an error something like “myArray not defined”.
I think the confusion comes from the fact that the tests’s function calls pass in myArray as an argument, and if someone try to just copy and paste those to test, one get a Reference Error
I would be really confused as to which myArray the test would be referencing… It can’t be the function parameter as that is local to the function and so the tests can’t check, right?
saying “the input array” would be less ambiguous, I think. Maybe not less confusing, tho
I say no less confusing because this challenge, after a series of challenges that have a function with a clear input and a clear output, present a situation in which you need to change the input, and there is no output. It sort of breaks the pattern, without stating it clearly, and makes hella confusing the test requirements.
Yeah, this particular challenge was not one that I thought should be included. I think we need to rethink this particular challenge or remove it completely as it does not flow like the other two recursion challenges.
@ilenia@ArielLeslie I have created PR #37548 which attempts to rectify some of the the confusion in the Use Recursion to Create a Countdown. You can click on the Review in Gitpod and view the challenge rewrite as it would appear in production. I welcome any comments or suggestions for the challenge. I literally spent about 5 minutes on the rewrite.