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??
if.