JavaScript | 자바스크립트 | CONTROL FLOW 넷
JavaScript | 자바스크립트 | CONTROL FLOW 넷
Code Your Own Adventure 2! | Codecademy
One. Choosing with Switch, And, & Or
File1 _ script.js
var troll = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();switch(troll) {
case 'FIGHT':
var strong = prompt("How courageous! Are you strong (YES or NO)?").toUpperCase();
var smart = prompt("Are you smart?").toUpperCase();
if(strong === 'YES' || smart === 'YES') {
console.log("You only need one of the two! You beat the troll--nice work!");
} else {
console.log("You're not strong OR smart? Well, if you were smarter, you probably wouldn't have tried to fight a troll. You lose!");
}
break;
case 'PAY':
var money = prompt("All right, we'll pay the troll. Do you have any money (YES or NO)?").toUpperCase();
var dollars = prompt("Is your money in Troll Dollars?").toUpperCase();
if(money === 'YES' && dollars === 'YES') {
console.log("Great! You pay the troll and continue on your merry way.");
} else {
console.log("Dang! This troll only takes Troll Dollars. You get whomped!");
}
break;
case 'RUN':
var fast = prompt("Let's book it! Are you fast (YES or NO)?").toUpperCase();
var headStart = prompt("Did you get a head start?").toUpperCase();
if(fast === 'YES' || headStart === 'YES') {
console.log("You got away--barely! You live to stroll through the forest another day.");
} else {
console.log("You're not fast and you didn't get a head start? You never had a chance! The troll eats you.");
}
break;
default:
console.log("I didn't understand your choice. Hit Run and try again, this time picking FIGHT, PAY, or RUN!");
}
2/6 Prompt
File1 _ script.js
var user = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?");
3/6 .toUpperCase() and .toLowerCase()
File1 _ script.js
var user = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();
4/6 Switch
File1 _ script.js
var user = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();
switch (user) {
case 'FIGHT':
console.log("You are a braveman.");
break;
case 'PAY':
console.log("Oh my money.");
break;
case 'RUN':
console.log("Move!");
break;
default:
console.log("What?");
}
5/6 Logical operators
File1 _ script.js
var user = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();
switch (user) {
case 'FIGHT':
var strong = prompt("Are you strong (YES or NO)?" ).toUpperCase();
var smart = prompt("Are you smart?").toUpperCase();
if (strong === 'YES' && smart === 'YES') {
console.log("You are a braveman.");
} else {
console.log("Run away!");
}
break;
case 'PAY':
var money = prompt("Do you have money (YES or NO)?").toUpperCase();
var dollars = prompt("Is your money in Troll Dollars?").toUpperCase();
if (money === 'YES' || dollars === 'YES') {
console.log("You are a rich.");
} else {
console.log("Oh my money.");
}
break;
case 'RUN':
console.log("Move!");
break;
default:
console.log("What?");
}
6/6 You did it!
Next Course: Arrays and Objects in JS
