Implement these language features:
the equality comparison operators == and !=
var a = 1; var b = 2; a == b; // #false a != b; // #true
conditionals: if with optional else
var a = 1; var b = 2; if ( a != b ) { alert("neq"); } if ( a == b ) { alert("eq"); } else { alert("still neq"); }
Result:
ALERT! neq ALERT! still neq
conditional loop: while
var a = 0; while ( a != 5 ) { alert(a++); }
ALERT! 0 ALERT! 1 ALERT! 2 ALERT! 3 ALERT! 4
PS. if and while blocks don’t need to be terminated by semicolons.
Add the relational operators < <= > >=.
var a = 0; var b = 1; a < b; // #true a <= b; // #true a > b; // #false a >= b; // #false
Add the relational operators && and ||.
var a = 0; var b = 1; a < b || a > b; // #true a < b && a > b; // #false
Add the ternary conditional operator.
var a = 0; var b = 1; a < b ? "yes" : "no"; // "yes"
Hint: There’s precedence among relational operators too.