Conditional Statements

不管 function 中有多少個 if else 在判斷,其實可以導向判斷各個狀況是 true or false,會比較好釐清。

一些常碰到的判斷問題:

100 >= 100 // 答案是 true

‘Apple’ < ‘Bear’ //答案是 true 因為這邊字母 A < B (後來的比較大)

Letter strings will always come after number strings too → If you compared a number and a letter, a number is less than a letter 字母永遠會大於數字

“3” == 3 //答案是 true,JS 會自己 convert string

“3” === 3 // 答案是 false,因為一個是 string 一個是 number,不同類型的 value 所以不同

**通常建議用 === 而不要用 == ,有可能會碰到怪怪狀況

比如:

‘ ‘== 0 // true

‘ ’=== 0 // false

Python’ === ‘Ruby’ // false 兩者根本就不同的 string

‘Javascript’ === ‘javascript’ // false 大小寫要一樣

‘10’ !== 10 // true ,因為一個是 string 一個是數字

簡單的 if else 範例

var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt(‘I am thinking of a number between 1 and 6. What is it?’);
if( parseInt(guess) === randomNumber ) {
document.write(‘<p>You guessed the number!</p>’);
} else{
document.write(‘<p>Sorry! The number was ‘ + randomNumber + ‘</p>’);
}

簡單的 if else &boolean 判斷的範例

if ( true ) {
document.write(‘The condition is true’);
} else {
document.write(‘The condition is false’);
}

延伸 boolean 用法

var correctGuess = false; // 一開始先讓某猜對的變數是 false (因為還沒開始猜)
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
if( parseInt(guess) === randomNumber ) {
correctGuess = true; //猜的數字跟 randomNumber 一樣時 correctGuess 這個變數才會變 true
}

// 接著就會出現第二個 if → 當 correctGuess === true 的狀況,會是怎樣,可以直接放 correctGuess
if(correctGuess){
document.write(‘<p> You guessed the right number!</p>’);
} else {
document.write(‘<p> Sorry. The number was ‘ + randomNumber + ‘. </p>’);
}

***Programming Multiple Outcomes***

If → 想像是一開始就成立的 door number 1

else if → 一定會先測試第一個狀況,當第一個狀況 = false 才會來到這裡,door number 2

else → 一樣是當 door number 1 和 door number 2 = false 時,才會跑這邊 door number 3

*注意!如果所有狀況都已經是 true 了,就不用有 else 出現

範例一:

var isStudent = false;
if ( isAdmin ) {
alert(‘Welcome administrator’);
} else if (isStudent) {
alert(‘Welcome student’);
} else {
alert (‘Who are you?’);
}

另一個範例:

var itemToBuy = ‘’;
var savings = 1000;
if ( savings > 500 ) {

itemToBuy = ‘Computer’;
} else if ( savings > 200 ) {
itemToBuy = ‘Phone’;
} else if ( savings > 0 ) {
itemToBuy = ‘Dinner’;
} else {
itemToBuy = ‘…still saving…’;
}

這邊跑出來的結果會是 computer,因為當第一項成立後,後面就不要管惹!

***In any conditional statement with multiple clauses only 1 code block will ever run.

A Random Number Guessing Game 範例(增加兩個 else if):

var correctGuess = false; // 一開始先讓某猜對的變數是 false (因為還沒開始猜)
var randomNumber = Math.floor(Math.random() * 6 ) + 1;

var guess = prompt(‘I am thinking of a number between 1 and 6. What is it?’);
if( parseInt(guess) === randomNumber ) {
correctGuess = true; //猜的數字跟 randomNumber 一樣時 correctGuess 這個變數才會變 true
} else if ( parseInt(guess) < randomNumber){

var guessMore = prompt(‘Try again! The number is more than ‘ + guess);
if(parseInt(guessMore) === randomNumber){
correctGuess = true;
}

} else if ( parseInt(guess) > randomNumber){
var guessLess = prompt(‘Try again! The number is less than ‘ + guess);

if(parseInt(guessLess) === randomNumber){
correctGuess = true;
}
}

// 新的 if / else 宣告,這邊是當面的結果都導到 correctGuess === true 的時候會發生的狀況
if(correctGuess){

document.write(‘<p> You guessed the right number!</p>’);

} else {

document.write(‘<p> Sorry. The number was ‘ + randomNumber + ‘. </p>’);
}

Combining Multiple Tests Into a Single Condition

可以用 && (所有都要成立) 或是 | | (其中一者成立即可)