Pushing more than 3 arrays inside of an array causes unexpected output

The Following Function returns 3 arrays only and the remaining are substituted with [Array]/

Tell us what’s happening:
when i try to push more than 3 arrays inside of an array it chops the remaining pushes and substitute it with [Array] as following.

Your code so far


/function countDown(n) {
  if (n < 1) {
      return [];
  }
  var temp  = [n];
  temp.push(countDown(n-1));
  return temp; //[ 6, [ 5, [ 4, [Array] ] ] ]

}
console.log(countDown(5));

I believe that is just a formatting decision.

In the future, please do not make a large number of topics all related to a single challenge.

1 Like

First, I’m sorry about the enormous question regarding the same question but i was struggling the entire day as i made 7 solutions and they all failed in the syntax only and hence why i did ask more than a question regarding the same topic.

I still don’t get the answer; what formatting?

Syntax can be pretty important - computers can do only and exactly what we say.

What I mean by formatting is that I think that console.log will only show nested arrays to a certain level. It doesn’t mean the data isn’t there, it just means that the console.log won’t show all of the data.

1 Like

Thanks sir. If i may ask, if i have related questions to the same topic, should ask it here or do something else?

Hi @Omar_Shawky!

If your follow up questions have to deal with the same challenge then post it here.

If your follow up questions have to deal with recursion in general than you can create a new topic.

1 Like

Tell us what’s happening:
the return/output returns ,,… without the square brackets []
So briefly:
Expected output is array: [3,2,1]
Actual output is: 3,2,1

Your code so far


// Only change code below this line
function countdown(n) {
    // If the function is called with a number less than 1, the function should return an empty array.
    if (n < 1) {
        return [];
    }

    //Else recursion to return an array containing the integers n through 1.For example, calling this function with n = 5 should return the array [5, 4, 3, 2, 1].
    // console.log("I will push n:" + n + " to temp: " + temp);
    var temp = [n];
    // console.log("Now temp is: " + temp);

    var remainingCount = countdown(n - 1);
    // console.log("RemainingCount: " + remainingCount);

    //if n = 1 ;do not add the empty array to my remaining count. This condition only and main purpose is t chop the empty array value form the output array.
    if (n != 1) {
        // temp.push(parseInt(remainingCount));
        temp.push(remainingCount);
    }

    return temp;
}

console.log("countdown: " + countdown(3));
  // Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

So, I think there is a simpler approach where the FCC sample can help you.

Here is the sample for the lesson.

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]

It has a simple base case

  if (n < 1) {
    return [];
  }

and simple recursive case

else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }

So if we want to create a countdown function we can use the same approach for the countup function but instead of adding elements to the end of the array (like in the sample) we can use another array method that add elements to the beginning of the array.

Make sense?

1 Like

I failed to do so (add elements to the beginning of the array), and instead i made a tweak and i actually was able to write it in a single line in java (using syntactic sugars) and i was afraid to do the same in javascript as things are not consistent in language.

briefly i mean, i know the concept and i can apply it but i cannot do the specific thing that you mentioned (add elements to the beginning of the array); could you please illustrate more?

2 Likes

So if you click on the link @JeremyLT gave you and tweek just one line in the sample (// hint it is this one : countArray.push(n)) and use the console.log then you will see the results you are looking for.

Now, there are of course multiple ways to solve this challenge but I think this is a simple approach :grinning:

1 Like

Once you solve the challenge, you can take a peek at solutions 3 and 4 if you are curious in one line answers

1 Like

I am Extremely thankful for your contribution and help; i was more concentrating on solving it in different ways to prove that i really learned javascript syntax (not just imitating/mimicking previous lessons); hence why i solved it with multiple ways/techniques. i did see this solution and i solved using it and syntactic sugars (from Java) but the others (the ones i asked about here in the forum) didn’t add up to what i learned due to the “inconsistency” in javascript.
So briefly, i wasn’t just “trying to find a way to pass the test” and i am still finding an explanation to my remaining solutions. please accept my apology to take form you that much of time :slight_smile:

You might look into trying code wars, where you can work through coding challenges and in both javascript and java and look at others solutions to see different approaches to the same problem.

This will expose you to other language features.

Some of the solutions are kind of cool and creative while some of the solutions are straight up weird or overly complicated :laughing:

But it is still a good resource.

1 Like

I did check those solutions and i did not understand solution number 4. what does the 3 dots ... mean? I already read that they are spread & rest operators and i understood their usage inside of an object (specific examples that i understood are retrieved from here [medium article for 3 dots](what does the 3 dots ... mean? I already read that they are spread & rest operators and i understood their usage inside of an object (specific examples that i understood are retrieved from here 3 dots medium article ). but i do not know it’s usage in the code below.


function countdown(n){
   return n < 1 ? [] : [n, ...countdown(n - 1)];
} 

I’m not quite sure what you mean by JavaScript inconsistencies? All high level languages have Syntactic Sugar. It’s a term that refers to language features that represent more complex concepts or operations.

It’s a question of learning the idioms in the language as you’re using to express the ideas you need, but anything you can do with Syntactic Sugar you can also do without.

This says ‘return an empty array if n is less than 1, otherwise return an array with n followed by all of the elements from the array returned as a result of countdown(n-1)’

2 Likes

inconsistency can be clear in the way object is declared or “the multiple ways to access an array” (using [] or .); push is a function that should never return anything; like wise i should never expect it to return “the length of array”. in a consistent language, you would be given -to be the least- an error regarding the incompatible types (you are returning a different type than the reference in case of Java)

When in doubt about what a language does, consult the documenation. For example, the push method in PHP also returns the length of the modified array. Different languages are different. Java is not JavaScript.

1 Like

when an array is concatenated to a string it is converted to a string becoming a string with a comma separated list

the output from the function it’s still an array

1 Like