#FreeCodeCamp 111/113

178 messages · Page 1 of 1 (latest)

kindred token
#

How can I use this method to make the code count down instead of up? function countdown(n){ return (n < 1) ? [] : [] ; }

#

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 */ } }

slim ermine
kindred token
#

so I don't want the answer

#

I want to figure it out

#

i need hints

#

also i haven't learned es6 yet

slim ermine
#

its basically the same but you have to look for another way to add the numbers to the array

kindred token
#

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

slim ermine
#

you don't need that
there is literally one thing you have to change compared to the example method

kindred token
#

i see what you did there

#

oh really?

#

fuck

slim ermine
#

ignore what I did, this is not what they want you to do there

slim ermine
kindred token
#

my intuition is saying that i'm using the const a recursion incorrectly

slim ermine
#

you just have to thing about the ways to get data into an array

#

there is more than push

kindred token
#
  /* 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?

slim ermine
#

yeah

#

you dont need to reverse

#

you need to add the correct way

kindred token
#

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(); }

slim ermine
#

this is not recursion

kindred token
#

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

slim ermine
#

I mean, you reverse an empty array

kindred token
#
  const a = [];
  return (n >= 1) ? a: a.reverse();
  }```
#

this still doens't work

slim ermine
#

you still reverse an empty array

kindred token
#

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

slim ermine
#

no
if its >=1 you reverse the array
and the array is empty

#

[] = empty

kindred token
#

then how do i put n inside the array?

#

bruh freecodecamp doesn't explain things very well

slim ermine
#

by using methods like push

#

you only have to think about the order

#

you get your numbers

kindred token
#

function countdown(n){
const a = [];
return (n >= 1) ? a.push(n) : a.reverse();
}

slim ermine
#

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

kindred token
#

.unshift
.push
.splice

#

that's all i can find

slim ermine
#

try

kindred token
#
  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;
  }```
slim ermine
kindred token
#

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,...]???

slim ermine
#

there will be no 0 cause of your condition
n < 1

kindred token
#

but i'm trying to use n >= 1

#

push at the end of the array

slim ermine
#

use the example one
if you got it there
try to use your own conditions

kindred token
#

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

slim ermine
#

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

kindred token
#

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

slim ermine
#

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]
...

kindred token
#

1-1 = 0

slim ermine
#

yeah

kindred token
#

0 - 1 = -1 and returns empty

slim ermine
#

no

kindred token
#

so if i put 1 in you would get 0 in the array

slim ermine
#

no you get []

kindred token
#

?????

#

but 1 is not less than 1

#

they're equal

slim ermine
#

oh wait, 1 will be [1]

kindred token
#

no it should be zero

slim ermine
#

no

kindred token
#

cuz 1-1=0

slim ermine
#

yeah but countup(0) returns [] and not [0]

kindred token
#

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

slim ermine
#

yeah it calls itself until it reaches the 0, then adds up the array

kindred token
#

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```
slim ermine
#

its countup without push

kindred token
#

how do i put the variable in if i don't use push

slim ermine
kindred token
#

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

slim ermine
#

bro just use the example

#

there is no need for a reverse

kindred token
#
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray.reverse();
  }
  }```
#

why tho

slim ermine
#

because you have unshift

kindred token
#

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

slim ermine
#

why is it easier to use?

#

it is just shorter

#

and you should use it for simple statements only

kindred token
#

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?

slim ermine
#

because you can create variables and call methods as you like

kindred token
#

so it's more flexible?

slim ermine
#

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

kindred token
#

so when is it better to use ternary over the others?

kindred token
#

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

slim ermine
#

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

kindred token
#

that actually makes a lot of sense though

slim ermine
#

but if you try more complex conditions like the countdown one, you are just overthinking and wasting time

kindred token
#

i think i just learned that the hard way lmao

#

well thank you for your help and time