switchで文字列が使えるのはJavaと違って便利よいね。
3項演算子も使い慣れると便利。これはJavaにもある。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. More on the "if" statement | |
var yourName = ""; | |
var result = ""; | |
if (0 === yourName.length) { | |
result = "What is your name?"; | |
} | |
// 2. Matching a condition or doing something else | |
var yourName = ""; | |
var result = ""; | |
// use the > symbol to check the length of yourName | |
if (0 < yourName.length) { | |
result = "Hi "+ yourName; | |
} else { | |
result = "What is your name?"; | |
} | |
// 3. Testing one or more conditions | |
var yourName = "Hanako"; | |
var gender = "female"; | |
var result; | |
if ("male" === gender) { | |
result = "His name is "+yourName; | |
} else if ("female" === gender) { | |
result = "Her name is "+yourName; | |
} else { | |
result = "Hi "+yourName; | |
} | |
// 4. Checking a condition for at least one result | |
var yourName = "Taro"; | |
var gender = "male"; | |
// don't forget to use the || operator | |
if ( "male" === gender || "female" === gender ) { | |
result = true; | |
} else { | |
result = false; | |
} | |
// 5. Checking for two conditions | |
var yourName = "Taro"; | |
var gender = "male"; | |
var result; | |
if (0 < yourName.length && 0 < gender.length) { | |
result = "Thanks"; | |
} else { | |
result = "Please make sure both yourName and gender are filled in."; | |
} | |
// 6. Nesting conditional statements | |
var yourName = "Taro"; | |
var gender = "male"; | |
var result; | |
//Line 10 starts an if statement | |
//Nested in this if statement is an if else statement on lines 11 - 15 | |
//This nested if else statement allows us to check another condition | |
//We close the first if statement at the start of line 16 | |
if (0 < yourName.length && 0 < gender.length) { | |
if ("male" === gender || "female" === gender) { | |
result = "Thanks"; | |
} else { | |
result = "Please enter male or female for gender."; | |
} | |
} else { | |
result = "Please tell us both your name and gender."; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Using if else statements in functions | |
var checkNameGender = function (yourName,gender) { | |
//All the code below was used in exercise 1.6 | |
if (gender.length > 0 && yourName.length > 0) { | |
if (gender === 'male' || gender === 'female') { | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
}; | |
checkNameGender("Taro", "male"); | |
// 2. Using if else statements within functions | |
var legalDrivingAge = 18; | |
var canIDrive = | |
// write if else statement here. Return true or false | |
function(myAge){ | |
return legalDrivingAge <= myAge; | |
}; | |
canIDrive(29); | |
// 3. Functions in if else statements | |
// Define the function under this line | |
var canIDrive = function(myAge, legalDrivingAge){ | |
return legalDrivingAge <= myAge; | |
}; | |
// Declare legalDrivingAge under myAge | |
var myAge = prompt("How old are you?"); | |
var legalDrivingAge = 18; | |
//Create an if else statement using the function as a condition | |
if (canIDrive(myAge, legalDrivingAge)) { | |
console.log("You can legally drive!"); | |
} | |
else { | |
console.log("You'll have to wait a few more years!"); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Background to switch statements | |
var jacketColor = "green"; | |
var result; | |
switch (jacketColor) { | |
case "black": | |
result = "Pay $300"; | |
break; | |
case "brown": | |
result = "Pay $200"; | |
break; | |
case "green": | |
result = "Pay $5"; | |
break; | |
default: | |
result = "This color does not match my eyes!"; | |
} | |
// 2. Introducing the switch statement | |
var myCar = "Toyota"; | |
var result; | |
switch (myCar) { | |
case "Ford": | |
result = "American brand"; | |
break; | |
case "Toyota": | |
result = "Japanese brand"; | |
break; | |
default: | |
result = "I'm not sure what country that car is from"; | |
break; | |
} | |
// 3. Write your own switch statement | |
var born = prompt("What country were you born in?"); | |
var result; | |
switch (born) { | |
case "USA": | |
result = "Born in the USA"; | |
break; | |
default: | |
result = "Born outside the USA"; | |
break; | |
} | |
// 4. Switch it up | |
var born = prompt("What country were you born in?"); | |
var result; | |
// write the whole switch statement | |
switch(born) { | |
case "USA": | |
result = 1; | |
break; | |
case "England": | |
result = 2; | |
break; | |
default: | |
result = 3; | |
break; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Introducing the shortcut | |
var x = 2; | |
var y = 1; | |
if (x > y) { | |
result = "good job"; | |
} | |
else { | |
result = 20; | |
} | |
//Below is the above code written using the ternary operator | |
result = x > y ? "good job" : 20; | |
// 2. Practice | |
var myAge = 29; | |
// a simple if else statement | |
if (myAge >= 18) { | |
answer = "adult"; | |
} | |
else { | |
answer = "non-adult" ; | |
} | |
// rewrite the code above using a ternary operator | |
answer = 18 <= myAge ? "adult": "non-adult"; | |
// 3. More practice! | |
//Original if else statement | |
//Rewrite the above code using the ternary operator | |
result = "Nick" === name ? true: false; | |
// 4. Setting values using a ternary operators | |
var food = prompt("What food do you like ?"); | |
var foodType; | |
if (food === "taco") { | |
foodType = "Mexican"; | |
} else { | |
foodType = "other"; | |
} | |
//Re-write the above code using a ternary operator | |
foodType = "taco" === food ? "Mexican": "other"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Conditionals | |
var gender = "female"; | |
if (gender === "female") { | |
result = "true"; | |
} | |
else { | |
result = "false"; | |
} | |
// 2. Nested Conditionals: Part 1 | |
var gender = "female"; | |
if (gender === "female") { | |
result = "true"; | |
} | |
else { | |
result = "false"; | |
} | |
// 3. Nested Conditionals: Part 2 | |
var topThree = "true"; | |
var winner = "false"; | |
var result; | |
if (topThree === "false") { | |
result = "Sorry, empty handed"; | |
} | |
else { | |
if (winner === "true") { | |
result = "Gold!!!"; | |
} else { | |
result = "Not bad! Got a medal!"; | |
} | |
} | |
// 4. Switch Statements | |
var jacketColor = "black"; | |
var result; | |
switch (jacketColor) { | |
case "black": | |
result = "Pay $300"; | |
break; | |
case "brown": | |
result = "Pay $200"; | |
break; | |
case "green": | |
result = "Pay $5"; | |
break; | |
default: | |
result = "This color does not match my eyes!"; | |
} | |
// 5. Ternary operator | |
var x = 10; | |
var y = 20; | |
//Rewrite the ternary code using if else statements | |
if(x > y) { | |
result = "good job"; | |
} else { | |
result = 20; | |
} |
0 件のコメント:
コメントを投稿