Tell us what’s happening:
I don’t know the algorithm or logic of this function. I write some console log for the intercept.
- Why counting back 5 ot 1 first and not continue the code?
- Why continue from console.log else2 when the code reach n<1?
- Why this “return ;” (in the fifth line) in if statemant?
- Why not good the plain “return;”?
- What connect " return;" and “return countArray;”?
- Why undefined countArray while n reach 0?
- What is the logic all of these?
thank you
This is the output to console:
countUpMain
n else1111111111111111111111111111111111111: 5
undefined
countUpMain
n else1111111111111111111111111111111111111: 4
undefined
countUpMain
n else1111111111111111111111111111111111111: 3
undefined
countUpMain
n else1111111111111111111111111111111111111: 2
undefined
countUpMain
n else1111111111111111111111111111111111111: 1
undefined
countUpMain
n: 0
n else2222222222222222222222222222222222222: 1
CountArraypushBefore:
CountArraypushAfter: 1
n else2222222222222222222222222222222222222: 2
CountArraypushBefore: 1
CountArraypushAfter: 1,2
n else2222222222222222222222222222222222222: 3
CountArraypushBefore: 1,2
CountArraypushAfter: 1,2,3
n else2222222222222222222222222222222222222: 4
CountArraypushBefore: 1,2,3
CountArraypushAfter: 1,2,3,4
n else2222222222222222222222222222222222222: 5
CountArraypushBefore: 1,2,3,4
CountArraypushAfter: 1,2,3,4,5
[ 1, 2, 3, 4, 5 ]
Your code so far
function countup(n) {
console.log("countUpMain");
if (n < 1) {
console.log("n: " + n);
return [];
} else {
let countArray;
console.log("n else1111111111111111111111111111111111111: " + n);
console.log(countArray);
countArray = countup(n - 1);
console.log("n else2222222222222222222222222222222222222: " + n);
console.log("CountArraypushBefore: " + countArray);
countArray.push(n);
console.log("CountArraypushAfter: " + countArray);
return countArray;
}
}
console.log(countup(5));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36
.
Challenge: Use Recursion to Create a Countdown
Link to the challenge: