Conditional Ternary Recursive Factorial? (JS)

javascript

#1

Hi everyone,

I'm currently trying (emphasis "try") to understand recursion, and on the JS factorial exercise recently attempted my first JS conditional ternary as well by rewriting the program in that format:

function ternaryTest(n) {
    n < 0 ? console.log("stahp") : n === 0 ? return 1 : return (n * ternaryTest(n-1));
}

However I keep getting errors (mostly unexpected return). I tried reading up about it but didn't see any results directly related to my case (i.e. using a return with the conditional operator without trying to assign it to a variable) -- er....well, at least I don't think they were.... :smiley:

Is my syntax wrong? Or can I just not use return with the conditional operator? Some other problem?

Thanks all!
frisby


PS: How do I add a "JS" tag to my post in this section? Or is that beyond my ability? :relaxed:


#2

I guess its better to use

return condition?ValueA:ValueB;

instead of:

condition?return ValueA:return ValueB;

And you should be able to apply tags by adding them in the text field directly over the submit button (if there is one. Maybe a screenshot of your form?).


#3

Thank you for your help, @haxor789! :slight_smile:

(And thank you @jibblyj for adding the tag for me :slight_smile:)

So in my case would it be like [...] n === 0 return ? 1 : (n * ternaryTest(n-1));?

Edit: Whoops, I guess it should have been [...] return n === 0 ? 1 : (n * ternaryTest(n-1));, but I must have done something else wrong too because I'm getting syntax error)

And would that mean that I can't return in the first case and do something else in the second?

I can't find the space for the tags; I haven't been able to upload pictures here today for some reason (stuck at 100%) but I will try again in a few hours and see if it works then.


#4

It seems like you cannot use return in ternaries like you'd do in regular if/else. So I'd suggest the way mentioned in the first post where you start your statement with return and let the ternary decide what value gets returned.


#5

Thank you, @haxor789.

I'm unsure whether I applied your suggestion properly; I wrote:

 function ternaryTest(n) {
     n < 0 ? console.log("stahp") : return n === 0 ? 1 : (n * ternaryTest(n-1));
 }

The console says: SyntaxError: Unexpected token return

Both this way and the original way the warning I see reads "expected an identifier and saw return."

^^^(I wasn't sure whether the return would go at the beginning of the current conditional operator or at the very, very beginning. I am guessing the latter is what you meant. What does "identifier" mean?)

I tried:

function ternaryTest(n) {
    return n < 0 ? console.log("stahp") : n === 0 ? 1 : (n * ternaryTest(n-1));
}

and putting the return at the very beginning like so instead of whilst chaining appeared to work (was unsure whether I was meant to put at the very beginning due to resulting return console.log). Yay! :raised_hands:


Also, this is what my form looks like:


#6

Yes I meant the latter one and yes return console.log isn't that nice. Also I hope that nested ternaries are only for practicing purpose as they can get pretty lengthy and unreadable when nested which is pretty much the opposite of what they are meant to be :slight_smile:

For identifiers, not that familiar with this so just a best guess from google:
http://docstore.mik.ua/orelly/webprog/jscript/ch02_07.htm