Learn JavaScript Properly 1-6 Codecademy JS Track (C)
4. Control Flow
SWITCH
- We already know
if,elseand loops(for,while). - If there are multipule/many choices, we can use
switchstatement. - syntax:
var thisVar;
switch(thisVar) {
case 'OPTION1':
ACTION1;
break;
case 'OPTION2':
ACTION2;
break;
default:
defaultACTION;
}
Logical Operators
- and(
&&), or(||), not(!). X && Ywill be true only ifXandYare both true.X || Ywill be true if one ofXorYis true, or both true. (Will only be false ifXandYare both false.)!true== false,!false== true.
Build “Choose Your Own Adventure”
- Content not really interesting, it doesn’t guide you through the thoughts of the programe design…… just note some points:
- use
.toLowerCase()or.toUpperCase()to make the string all lower or upper case, so the comparison will not be case-sensitive. - The game structure: 1. A
promptfor 3 choices. 2. Aswitchstatement to determin what to do in all cases. 3. Each choice have anif…elsestatement to let user choose from.