Control Flow If/Else


I have entered my code over and over again, with spacing differences, etc, but every way I play around with it still presents this error:
if (is SoccerFan === true) {console.log(‘Goal!’);} else {console.log(‘No goal!’);}
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

My code:

 let isSoccerFan = true;
if (isSoccerFan === true) { console.log('Goal!'); } else { console.log('No goal!'); }

It’s such a simple exercise and a no-brainer, but I’m majorly stuck.


The code above throws a syntax error because is is separate from SoccerFan.

The code in your example runs and passes.


In structures that use blocks, don’t cheap out on white space.

if (isSoccerFan) {
} else {
  console.log('No goal!');

Note that when the variable is a boolean, we do not need to compare to true. The conditional will yield the boolean.

Extra study

Set this aside for the time, but expect to see it in your learning path. Once you get to that point and are comfortable with control flow and conditionals, you will find lots of opportunities to use ternary expression statements

let isSoccerFan = Math.floor(Math.random() * 2);

// above will be 0 or 1
// in a conditional, 0 casts to false, 1 casts to true

console.log(isSoccerFan ? 'Goal!' : 'No goal!');

For now, though, don’t toss the if statement aside and use it in the lessons when expected to. In later lessons we have a smidgeon of creative license but still have the SCT to contend with. Play with your lesson code in a sandbox or on your own computer as you learn each concept. FIfteen minutes now will save you an hour or more down the road.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.