Control Flow If/Else


#1

https://www.codecademy.com/courses/learn-javascript-control-flow/lessons/control-flow/exercises/if-else-statements?action=lesson_resume&course_redirect=introduction-to-javascript

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:
/home/ccuser/workspace/learn-javascript-U2L1E2/main.js:2
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.


#2

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

The code in your example runs and passes.

Aside

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

if (isSoccerFan) {
  console.log('Goal!');
} 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.


#3

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