Basic JavaScript: Use Recursion to Create a Countdown - Free Code Camp

preview_player
Показать описание
In this basic JavaScript tutorial we use recursion to create a countdown array as part of this series of free code camp tutorials. Recursion is tricky stuff so I hope you find this tutorial useful. Truth be told, I don't 100% get this.
Рекомендации по теме
Комментарии
Автор

function countdown(n) {
if (n < 1) {
return [];
} else {
const arr = countdown(n - 1);
arr.unshift(n);
return arr;
}
}
THIS WORKED SIR

sumitsinha
Автор

The current exercise uses only the n parameter. This works for it.
function countdown(n) {
if (n < 1) {
return [];
} else {
let arr = countdown(n - 1);
arr.unshift(n);
return arr;
}
}

artistpw
Автор

theyve changed this tutorial lesson since your video??
now they dont include myArray in the parameters and using your code exactly doesnt work :(

this makes -150 sense to me and i cant find any tutorials to explain why and how it works now with the changed tutorial and the hints dont make sense either - i want to learn, not have an answer

JWhitty
Автор

watching your videos helps me a lot to understand it better, thank you so much :) :)

caioitalo
Автор

hey, i think i fall into infinite recursion.
i write var arr = countDownNum(num ); instead of var arr = countDownNum(num - 1);
my freecodecamp lesson is crashed now because of infinite recursion and i cant edit it cz its crashed. i am stuck in hell. how i can get it back?

iffatfatima
Автор

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

Hello bro I don't understand how it's convert an array.

Step 1: if the statement is false and then else statement and const arr = countdown(5-1) // arr = countdown(4)
and it's countdown(3), countdown(2), countdown(1) and finally return [ ]. but how it's result [5, 4, 3, 2, 1]
else statement has no create array but push or unshift it. Please explained step by step.


a simple example:

let z = 3;
z.push(5);
console.log(z)


Output: An Uncaught TypeError: z.push is not a function

when I don't create an array and through an Error.

mdmydulislam
Автор

can u explain this thing with these else statements whats the point of them?

YoAbdi
Автор

Him: Wow, my code works.
Also Him: Hmm, how did this work, really?

Sorry, no offense.

sunjungwoo
Автор

Here is the current solution if anyone needs it, I don't know if this is the best solution but it works -
function countdown(n){
if (n < 1) {
return [];
} else {
const arr = countdown(n - 1);
arr.unshift(n);
return arr;
}
}
console.log(countdown(10));

destination
Автор

// Only change code below this line
function countdown(myArray, n){
if(n<=0){return 0;}
else{
myArray.push(n);
countdown(myArray, n-1);
}

}

// Only change code above this line




// running tests
countdown(-1) should return an empty array.
countdown(10) should return [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
countdown(5) should return [5, 4, 3, 2, 1]
// tests completed
NOT RUNNING

sumitsinha
Автор

follow this code and see the results :

function counDownNum(num) {
if(num <= 0) {
return []; // the condition here is false and should return empty array
} else {
var arr = countDownNum(num - 1);
arr.unshift(num);
return arr;
}
};

console.log(countDown(10));

The end of story !!!

mounirelbertouli