var userChoice = prompt("What would you like to play?"),
computerChoice = Math.random();
if (computerChoice <= 0.34){
computerChoice = "Rock";
}
else if (computerChoice >= 0.35 && computerChoice <= 0.67){
computerChoice = "Paper";
}
else {
computerChoice = "Scissors";
}
document.write("Computer's choice is " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2){
return "This result is a tie!";
}
else if (choice1 === "Rock") {
if (choice2 === "Scissors"){
return "Rock Wins!";
}
else {
return "Paper Wins!";
}
}
else if (choice1 === "Paper"){
if (choice2 === "Rock"){
return "Paper Wins";
}
else {
return "Scissors wins";
}
}
else if (choice1 === "Scissors"){
if (choice2 === "Rock"){
return "Rock WINS";
}
else {
return "Scissors wins";
}
}
};
document.write(compare(userChoice, computerChoice));
<!doctype html>
<html>
<title>JS Practice</title>
<head>
\t <script language="javascript">
</script>
</head>
<body>
\t
\t
</body>
</html>
請幫助我瞭解爲什麼它不顯示返回的字符串?當你贏或輸時,應該顯示結果。我從codeacademy得到了這段代碼。在他們的控制檯上工作完美,但是當你嘗試在瀏覽器上運行時,它不能正常工作。困惑......
var userChoice = prompt("What would you like to play?");
\t var computerChoice = Math.random();
\t if (computerChoice <= 0.34){
\t \t computerChoice = "Rock";
\t }
\t else if (computerChoice >= 0.35 && computerChoice <= 0.67){
\t \t computerChoice = "Paper";
\t \t }
\t else {
\t \t computerChoice = "Scissors";
\t \t } \t
\t document.write("Computer's choice is " + computerChoice);
\t var compare = function(choice1, choice2){
\t \t if (choice1 === choice2){
\t \t \t return "This result is a tie!";
\t \t }
\t \t else if (choice1 === "Rock") {
\t \t \t if (choice2 === "Scissors"){
\t \t \t \t return "Rock Wins!";
\t \t \t }
\t \t else {
\t \t \t return "Paper Wins!";
\t \t \t }
\t \t }
\t \t else if (choice1 === "Paper"){
\t \t \t if (choice2 === "Rock"){
\t \t \t \t return "Paper Wins";
\t \t \t }
\t \t \t else {
\t \t \t \t return "Scissors wins";
\t \t \t } \t
\t \t }
\t \t else if (choice1 === "Scissors"){
\t \t \t if (choice2 === "Rock"){
\t \t \t \t return "Rock WINS";
\t \t \t }
\t \t \t else {
\t \t \t \t return "Scissors wins";
\t \t \t }
\t \t }
\t };
\t compare(userChoice, computerChoice);
你如何在瀏覽器中運行它?在一個HTML頁面? – Netham
YE。從記事本++我運行它在鉻。 – Marin
請添加更多詳細信息:你的意思是說不能正常工作? – pietro909