Basic JavaScript: Use Recursion to Create a Countdown

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.

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

You have made it too complicated:

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.

5 Likes

Oh i didn’t think about the fact that i return somthing, in the challenge they do not say somthing about returning any value. My bad and thank you

If you don’t mind I will use your post. Why my solution does not work but locally in VS Code everything is OK

function countdown(myArray, n){
    var myArray = [];
      
    if(n <= 0){
      return myArray;
    }
    myArray.push(n);
    return myArray.concat(countdown(myArray, n - 1));
  };

why do I get this error when I run it in chrome console?

function countdown(myArray, n){

  if (n <= 0) {

    return [];
  } else {
    myArray.push(n);
    countdown(myArray, n - 1);
  }
};
countdown(myArray, 5);

VM707:9 Uncaught ReferenceError: myArray is not defined
at :9:11

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

1 Like

you call the function with a myArray as argument, but there is not a global myArray defined anywhere

2 Likes

countdown(myArray, 5); is outside of the function definition and is actually calling the function. You should use:

function countdown(myArray, n){
...
};
const myArray = [];
countdown(myArray, 5);

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);
function countdown(myArray, n) {
  if (n <= 0) {
    return [];
  } else {
    const myArray = countdown(myArray, n - 1);
    myArray.unshift(n);
    return myArray;
  }
}

Why is this solution wrong?

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.

2 Likes

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?

1 Like

Still curious about my last question…any help would be appreciated, thanks.

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

2 Likes

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.

I think it should be included somewhere, to present a totally different situation, but it needs or a different position, or some more explanation

2 Likes

Hey @ilenia, 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.

1 Like

This is the solution that i am referring to:

function countdown(myArray, n) {
  if (n <= 0) {
    return [];
  } else {
    const numbers = countdown(myArray, n - 1);
    numbers.unshift(n);
    return numbers;
  }
}

Thanks.

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].

@camperextraordinaire, should I abstain from using the same name for an object and a function parameter in the future?

I do not like the fact this question is based on mutating the original myArray variable.