2012年2月2日木曜日

Code Year 2012 - Week 3

3週目。復習が多くていまいち物足りない。
switchで文字列が使えるのはJavaと違って便利よいね。
3項演算子も使い慣れると便利。これはJavaにもある。


// 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.";
}
view raw 2.js hosted with ❤ by GitHub
// 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!");
}
view raw 3.js hosted with ❤ by GitHub
// 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;
}
view raw 4.js hosted with ❤ by GitHub
// 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";
view raw 5.js hosted with ❤ by GitHub
// 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;
}
view raw 6.js hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿