#FreeCodeCamp 111/113
178 messages · Page 1 of 1 (latest)
shouldn't it be counting down?
or wait
am i only putting in one number n?
this is what I'm trying to do: function countdown(n){ return (num < 1) ? /*put in array*/ : /*somehow reverse the order in the array */ } }
is n a number or an array?
if its a number:
function countdown(n)
{
return n < 1 ? [] : Array.from({length: n}, (_, i) => i + 1).reverse();
}
console.log(countdown(10));
if its an array:
let arr = [1, 2, 3, 4, 5];
console.log(arr.reverse());
so I don't want the answer
I want to figure it out
i need hints
also i haven't learned es6 yet
its basically the same but you have to look for another way to add the numbers to the array
this is kind of what i've got so far: function countdown(n){ const a = countdown(n-1); return (n < 1) ? [] : a.push(n).reverse(); }
it doesn't work ofc
part of the problem is that i reallyyyyyyy like using ternary
but i haven't really grasped the concept of recursion yet
so i have no idea how i'm supposed to use a tbh
what does array.from do?
ooooooh
you don't need that
there is literally one thing you have to change compared to the example method
ONE thing?
ignore what I did, this is not what they want you to do there
yes
my intuition is saying that i'm using the const a recursion incorrectly
you just have to thing about the ways to get data into an array
there is more than push
/* This is the recursion which makes the array 1 less than the length of n*/
const a = countdown(n-1);
return (n < 1) ? [] /* This puts all n greater than 1 into an unnamed array */
: a.push(n).reverse(); /*this is where I need to push the last number of n into the array, then reverse the order */
}```
but I can't figure out how to put the last statement into a new array
shouldn't the new array be a?
wait so i need to use something other than push?
ugh
i feel like this is a vocabulary problem
like i don't know what the word im trying to use in a sentence
dude i don't understand recursion
i don't get it
right now a is making my count down return an array of all n less than 1
lmao wait
no nvm
okay so the function right now is putting all n greater than 1 into an array
why can't i do something like this: function countdown(n){ return (n >= 1) ? [].reverse(); }
this is not recursion
lol yeah
i was thinking it would be simpler tho
return (n >= 1) ? []: [].reverse();
}
// Only change code above this line```
i don't understand why that doesn't work
I mean, you reverse an empty array
you still reverse an empty array
how is it empty?
i named the array
and the condition is that if n is greater than or equal to 1, put it in the array a
then how do i put n inside the array?
bruh freecodecamp doesn't explain things very well
by using methods like push
you only have to think about the order
you get your numbers
function countdown(n){
const a = [];
return (n >= 1) ? a.push(n) : a.reverse();
}
no push
push adds them at the end
you simply want something that adds them at index 0
so its will be
1
2,1
3,2,1
try
const a = [];
return (n >= 1) ? a.unshift(n) : a.reverse(n);
}```
still doesn't work
if .push puts things at the end of the array, why can't i just push the n into an array and not use a reverse
if n = 10 and i use a.push(n) why doesn't it just go from [10,9,8,7,6,5,4,3,2,1]
if the array is empty anyways
and i push it to the end
then 10 would go in first
the 9 would go next
and so on
const a = [];
return (n >= 1) ? a.push(n) : a;
}```
because of the recursion
you call the the function again and again before adding the first element
so you are actually starting with 0 and not 10
wait what?
why is this so hard for me to understand
so if an array is empty, it automatically will have a zero????
even so
it should go [10,9,8,7,6,5,4,3,2,1,0]
or is it [0,10,9,8,7,...]???
there will be no 0 cause of your condition
n < 1
use the example one
if you got it there
try to use your own conditions
but that doesn't make any sense
if n is less than 1 and n is positive, nothing would go in
nothing would happen if n = 5 cuz the condition isn't met
n has to be greater than 5
than 1 i mean
if ( 5 < 1) return empty array
this happens:
countup(5)
countup(4)
countup(3)
countup(2)
countup(1)
countup(0)
[]
[1]
[1,2]
[1,2,3]
[1,2,3,4]
[1,2,3,4,5]
the function call happens all over before adding the first element
in the example it says that, but i don't get how mathematically that makes any sense
if n is less than 1 return nothing
else, make new array where n-1
and you push n into the array
so push 10-1 = 9 into the array
countup(0) returns []
countup(1) uses the [] from countup(0) and returns [1]
countup(2) uses the [1] from countup(1) and returns [1,2]
...
1-1 = 0
yeah
0 - 1 = -1 and returns empty
no
so if i put 1 in you would get 0 in the array
no you get []
oh wait, 1 will be [1]
no it should be zero
no
cuz 1-1=0
yeah but countup(0) returns [] and not [0]
oh wait
the first condition is that since 1 is not less than 1
return 1 into the array
okay lmao that makes sense
okay hold up let me try your suggestion then
yeah it calls itself until it reaches the 0, then adds up the array
function countdown(n){
const a = [];
return (n < 1) ? a
: (n - 1) ? a.push(n)
: /* finish condition */
}
const a = [];
return (n < 1) ? a
: (n - 1) ? a.push(n)
: /* finish condition */
}```
const a = [];
return (n < 1) ? a
: (n - 1) ? a.push(n)
: a.reverse();
}```
that doesn't work though
countdown(10) should return [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
countdown(5) should return [5, 4, 3, 2, 1]
Global variables should not be used to cache the array.
// tests completed```
its countup without push
how do i put the variable in if i don't use push

i've tried all those
function countdown(n){
const a = [];
return (n < 1) ? a
: (n - 1) ? a.unshift(n)
: a.reverse();
}
none of them work
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray.reverse();
}
}```
why tho
because you have unshift
okay that worked
but i don't want to use an if statement
i wanted to do something like this: const reverse = (arr=[], num) =>{ return num === 1? [...arr, num]:reverse([...arr, num], (num - 1)) }
but i don't even understand how that works
i better understand how recursion works now though at least
i just wanted to use ternary cuz it literally just showed me that and now it wants me to revert back to if statements
if ternary is easier to use, why not just use that instead
why is it easier to use?
it is just shorter
and you should use it for simple statements only
exactly
if you can code faster, and it's easier to read
easier to manipulate later on
and it probably even makes the program run faster
why use if statements and loops and all those other longer more tedious methods?
because you can create variables and call methods as you like
so it's more flexible?
of course
using ternary you have to mesh everything together
how much time have you wasted by trying to write your solution in ternary instead of just using if
so when is it better to use ternary over the others?
3hrs
the site literally had me do ternary exercises like 2 practices before this one
why show it to me ONE time then go back to something else
at least explain the purpose and practical use of when they're applicable
ughhhh
lol
you should use it when if else seem unnecessary
if(myNumber < 5)
{
return "< 5";
}
else{
return "> 5";
}
here you don't have to thing about how to get this done with ternary and it would be smaller and as readable
return myNumber < 5 ? "< 5" : "> 5";
stupid example but I can think of something better right now
that actually makes a lot of sense though
but if you try more complex conditions like the countdown one, you are just overthinking and wasting time