[英]Can someone explain this block of code that contains an if else statement to me?
我正在重新學習 if / else 語句的基礎知識,因為它們是我在學校的弱點。
我正在代碼學院學習 java 腳本課程,我正處於 if 語句的位置。 我很困惑的例子是這樣的:
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
考慮到 userChoice 的 else 語句是否等於“剪刀”?
還是將 userChoice 切換為“paper”,將 computerChoice 切換為“rock”?
我知道這是非常基本的東西,但它總是讓我感到困惑。
整個function如下
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'It is a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
if (userChoice === 'paper') {
if (computerChoice === "scissors") {
return 'The computer won!';
} else {
return 'you won!';
}
}
if (userChoice === 'scissors') {
if (computerChoice === "rock") {
return 'The computer won!';
} else {
return 'you won!';
}
}
if (userChoice === 'bomb') {
return 'Bomb beats all';
}
}
這基本上就是您的if
語句所做的:
if (computerChoice === 'paper') {
return 'The computer won!';
} if (computerChoice !== 'paper') {
return 'You won!';
}
如果if
語句為假,則調用else
語句 - 因此,如果computerChoice
是不完全是paper
的東西,則調用else
語句。 標准if-else
將始終為 output,因為只有兩條路徑 - 相等或不相等。 中間沒有。
這是解釋:
注意: ===
檢查值及其類型
if (userChoice === 'rock') { // if **userChoice** is of string **rock** that means the if condition is true and it will continue execiting the code inside if
if (computerChoice === 'paper') { // if **computerChoice** is of string **paper** it executes code inside and returns 'the computer has won '
return 'The computer won!';
} else { // if **computerChoice** was not of **string** paper it executes 'You won' because the computer chose scissors
return 'You won!';
}
}
如果 userChoice 與 computerChoice 相同,則執行此代碼
if(userChoice === computerChoice){
return 'It is a tie!';
}
在此示例中,通常首先檢查userChoice
值,假設它不等於computerChoice
- 在這種情況下,我們檢查兩個值並將它們相互比較。
因此,在匹配第一個if
塊之后,將驗證第二個匹配,即查找computerChoice
的值。
讓我們假設這種情況: userChoice = 'scissors'
我們需要檢查computerChoice
的值是多少。 如果computerChoice
的值不是“剪刀”,那么我們知道它不會是平局。 我們必須尋找另一場比賽。
我們找到了符合第一個條件的塊:
if (userChoice === 'scissors'){}
現在我們需要看看這個if
語句里面有什么,我們有:
if (computerChoice === "rock"){
return 'The computer won!';
} else {
return 'you won!';
}
這意味着如果computerChoice = 'rock'
那么它將return 'The computer won!'
如果computerChoice
不是“搖滾”,則else
語句將運行,該語句將return 'you won!'
.
重要說明:代碼從上到下執行(幾乎沒有例外),因此首先檢查此塊:
if(userChoice === computerChoice){
return 'It is a tie!';
}
如果您將此塊移動到 function 的底部,您可能會看到意外的結果。
const determineWinner = (userChoice, computerChoice) => { if (userChoice === 'rock') { if (computerChoice === 'paper') { return 'The computer won;'; } else { return 'You won;'; } } if (userChoice === 'paper'){ if (computerChoice ==="scissors"){ return 'The computer won;'; } else { return 'you won:'; } } //found the match;., if (userChoice === 'scissors'){ if (computerChoice ==="rock"){ return 'The computer won!'; } else { //run else statement return 'you won!'; } } //it did not reach this point and returned before :( if(userChoice === computerChoice){ return 'It is a tie!'; //should be a tie } if (userChoice === 'bomb'){ return 'Bomb beats all'; } } console.log(determineWinner('scissors', 'scissors'))
我想強調這種行為,以便您牢記這一點。
我希望這是有道理的。
不,不是,這就是第 4 個 IF 語句的處理方式。 其結構化方式意味着每個 IF 塊僅處理兩個輸入。 如果用戶把S放在剪刀中,那么它將跳過那個IF塊
我會盡力解釋它。 對於這段代碼:
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
計算機按照語句的順序運行。 首先,他檢查用戶選擇了什么(在這個片段中很重要)。 if (userChoice === 'rock')
然后因為用戶確實選擇了rock,所以它會移動到下一個檢查計算機選擇的語句。 如果計算機選擇了紙(正確的計數器),它將移動到語句的返回,即計算機選擇了計數器並贏了
if (computerChoice === 'paper') {
return 'The computer won!';
但是,如果計算機選擇了除紙以外的任何其他內容,則跳過上面的 if 語句並執行 else 語句,即玩家獲勝
else {
return 'You won!';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.