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.
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.
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.
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.
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.
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?
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
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
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
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)’
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.