/*
Is that possible to do it with ternary operators ? I always get [-1] when I run the function countdown(-1)
*/
function countdown(n){
let countdownArray = [];
(n<=0 || n===1 || n==-1) ? []
: countdownArray = countdown(n-1);
countdownArray.unshift(n);
return countdownArray;
//return;
}
let result = countdown(-1);
console.log(result);
You shouldn’t use ternary operators as a general purpose replacement for an if statement. That’s not what they are for. They are for setting a value based upon a condition
const myValue = condition ? valueIfTruthy : valueIfFalsy;
1 Like
Ok, thanks for your reply… So this won’t work at all ?
Just would like to know if I misdid something in there.
You can force it work, but this is not how you should use a ternary.
1 Like
Thanks JeremyLT, I’m gonna try again, that just to see how it could work, so please do if you can help with any hint