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?
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.
Hey @ILM, you’re correct! My confusion stemmed from the fact that the test cases in FCC were written with myArray as an argument, testing with @camperextraordinaire cases in Chrome console worked but i was still getting one more error the function was returning as undefined.
This is the solution that FCC lists:
function countdown(myArray, n) {
if (n <= 0) {
return;
} else {
myArray.push(n);
countdown(myArray, n - 1);
}
}
So i rewrote FCC’s listed solution as:
function countdown(myArray, n) {
if (n <= 0) {
return;
} else {
myArray.push(n);
countdown(myArray, n - 1);
}
return myArray;
}
Now it returns the proper array! Thank you i’m pretty new at this and wanted to get my head around what was happening.
However, I’m still curious as to why the first solution i listed doesn’t pass the tests 2 and 3.
I think that tests descriptions are a little bit misleading.
When we are calling countdown myArray is somwhere defined. It could be: countdown([1,2,3], 10);
or
var myArray = [1, 2, 3];
countdown(myArray, 10);
So if you return an empty array inside an if block then basically you will reset myArray value and instead returning [10,9,8,7,6,5,4,3,2,1,1,2,3] you will get [10,9,8,7,6,5,4,3,2,1].