2014-10-12 48 views
0

如果userChoice爲空或除岩石,紙張或剪刀之外的單詞,我該如何停止執行此功能。 我試圖使用返回,但我無法讓它工作。 任何幫助表示讚賞。 感謝從Javascript中退出函數。簡單的搖滾,紙,剪刀遊戲

JS小提琴鏈接= http://jsfiddle.net/Renay/d9bea2ra/1/

var userChoice = prompt('Do you choose rock, paper or scissors?'); 
var compChoice = Math.random(); 

if (compChoice <= 0.34) { 
    compChoice = 'rock'; 
} else if (compChoice <= 0.67) { 
    compChoice = 'paper'; 
} else { 
    compChoice = 'scissors'; 
} 

function compare() { 
    if (userChoice == compChoice) { 
     console.log('The result is a tie!'); 
    } else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper')) { 
     console.log('You win!'); 
    } else { 
     console.log('You lose!'); 
    } 

    if (userChoice === null) { 
     console.log('Please select an option');  
    } else if (userChoice !== 'rock'&&'paper'&&'scissors') { 
     console.log('Please select rock, paper or scissors');  
    } 
} 

console.log('Your choice = ' + userChoice); 
console.log('Computer Choice = ' + compChoice); 
compare(); 
+0

你想在哪裏退出該功能? 'return'應該可以工作。 – Scimonster 2014-10-12 09:37:27

+0

在if語句中,userChoice === null以及userChoice!=='rock'&&'paper'&&'scissors' – Renay 2014-10-12 09:40:11

回答

1

您的if語句中的條件是錯誤的。它應該是:

if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') 

形式e1 && e2 && e3 && ...的表達式計算,如果所有的人都truthy持續eN子表達式。所以,你的測試是等價於:

if (userChoice !== 'scissors') 

你應該把那張支票之前顯示遊戲的結果,並從函數返回即可。所以它應該是:

function compare() { 
    if (userChoice === null) { 
     console.log('Please select an option');  
     return; 
    } else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') { 
     console.log('Please select rock, paper or scissors'); 
     return; 
    } 

    if (userChoice == compChoice) { 
     console.log('The result is a tie!'); 
    } else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper')) { 
     console.log('You win!'); 
    } else { 
     console.log('You lose!'); 
    } 

} 
+0

感謝您接受該錯誤。而且,我只是移動了我的代碼,它的工作!非常感謝。 – Renay 2014-10-12 09:56:40

0

只是做return;

function compare() { 
    if(!userChoice) 
    return; 
    // (...) more code 
} 

一個return調用退出函數。但請注意,您只能在function內使用return

0

如果我理解正確,您想返回到代碼的開頭。你應該換行整個事情的功能,然後再調用該函數:

function rockPaperScissors() { 
    var userChoice = prompt('Do you choose rock, paper or scissors?'); 
    var compChoice = Math.random(); 

    if (compChoice <= 0.34) { 
     compChoice = 'rock'; 
    } else if (compChoice <= 0.67) { 
     compChoice = 'paper'; 
    } else { 
     compChoice = 'scissors'; 
    } 

    function compare() { 
     if (userChoice == compChoice) { 
      console.log('The result is a tie!'); 
     } else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper')) { 
      console.log('You win!'); 
     } else { 
      console.log('You lose!'); 
     } 

     if (userChoice === null) { 
      console.log('Please select an option'); 
      rockPaperScissors(); 
     } else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') { // fixed 
      console.log('Please select rock, paper or scissors'); 
      rockPaperScissors(); 
     } 
    } 

    console.log('Your choice = ' + userChoice); 
    console.log('Computer Choice = ' + compChoice); 
    compare(); 
} 
rockPaperScissors(); 

您也有一個錯誤與檢查,如果它不是石頭,紙,或剪刀之一,所以我固定的。 (It seems Barmar got to it before me

+0

我試圖在一個函數中包裝整個事物,但由於某種原因它沒有執行代碼。並感謝您幫助解決這個錯誤。欣賞它。 – Renay 2014-10-12 10:00:28

+0

哦,是的,你也需要調用函數。 ;) – Scimonster 2014-10-12 10:00:58

+0

噢,我錯過了最後的電話哈哈。謝謝 :) – Renay 2014-10-12 10:03:45

0

我更新了功能的每個的if/else部分退出時,它匹配的if/else條件與return false。另外,我將if (userChoice === null)檢查更改爲if (userChoice === ""),因爲它看起來像prompt('Do you choose rock, paper or scissors?');在用戶沒有輸入任何值時返回空字符串。 Fiddle此外,我剛剛更新了函數的開頭,以便根據** Barmar'的建議(他第一次正確使用它)。

function compare() { 
    if (userChoice === "") { 
     console.log('Please select an option'); 
     return false; 
    } else if ((userChoice !== 'rock') && (userChoice !=='paper') && (userChoice !=='scissors')) { 
     console.log('Please select rock, paper or scissors'); 
     return false; 
    } 

    if (userChoice == compChoice) { 
     console.log('The result is a tie!'); 
     return false; 
    } else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper')) { 
     console.log('You win!'); 
     return false; 
    } else { 
     console.log('You lose!'); 
     return false; 
    } 
}