1

I am unable to figure out why I cannot use my for loop inside a ternary operation. Here is the code that is not working:

this.ask = function() {
  m = (isVoice) ? 'voice' : 'text';
  switch (true) {
    case m == 'voice' && typeof questions[timer.question].voice == 'string':
      (++timer.attempts > timer.maxAttempts) ?
        console.log('Stop'):
        console.log('Play file (' + timer.attempts + '): ' + questions[timer.question].voice);
      break;
    case m == 'voice' && typeof questions[timer.question].voice == 'object':
      (++timer.attempts > timer.maxAttempts) ?
        console.log('Stop'):
        for (i = 0; i < questions[timer.question].voice.length; i++) {
          console.log(questions[timer.question].voice[i])
        };
      break;
    default:
      (++timer.attempts > timer.maxAttempts) ?
        console.log('Stop'):
        console.log('Say Text (' + timer.attempts + '): ' + questions[timer.question].text);
      break;
  }
};

Specifically the case where m == 'voice' and typeof == 'object' throws the error "Uncaught SyntaxError: Unexpected token for". If I change that one case to be:

case m == 'voice' && typeof questions[timer.question].voice == 'object':
            console.log('Audio, Array.');
            if (++timer.attempts > timer.maxAttempts) {
                console.log('Stop');
            }
            else {
                for (i in questions[timer.question].voice) {
                    console.log(questions[timer.question].voice[i]);
                }
            }
            break;

... then everything works as expected.

Why is this??

3
  • In the ternary you need to use expressions that return some value, for loop does not return anything. Commented Jan 25, 2016 at 18:52
  • 1
    The ternary operator can be very handy but it also leaves a hole open for unreadable spaghetti code. In my humble opinion, this is one of those instances. I would not recommend writing code in this fashion. Use an if. Commented Jan 25, 2016 at 18:53
  • I went with what many had pointed out... simple if/else statements for cleaner code. Thank you all for the input. Thank you Pointy for explaining what the problem was. Commented Jan 25, 2016 at 22:00

2 Answers 2

2

The syntax for the ternary operator expects that the "branches" are expressions. You can't just put any arbitrary statement there; in JavaScript, a for loop is not an expression.

You could wrap the loop in a function and call it, but it would be a lot simpler to just use a plain if statement.

Sign up to request clarification or add additional context in comments.

Comments

0

if you wrap your for loop in parenthesis it might work.

function() {
    for (i = 0; i < questions[timer.question].voice.length; i++) {
     console.log(questions[timer.question].voice[i])
    }
}()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

But stylistically you're really pushing the envelope...

2 Comments

That too is a syntax error. The same one, in fact, and for essentially the same reason. Once you start the statement with (, the only thing that can follow is an expression, and for is not a token that can start part of an expression.
Right. As @Pointy states previously the only way one could achieve this would be with an anonymous function immediately executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.