这篇论文和摇滚迷你游戏有什么问题?

时间:2013-12-09 17:54:46

标签: javascript

它没有返回错误但我在codeacademy的控制台中感觉不对。它返回单弦 - “摇滚”,“纸”或“剪刀”。我无法发现什么是错的。

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

function compare(choice1, choice2){
    if(choice1 == choice2) return "The result is a tie!";

    if(choice1 == "rock"){
        if(choice2 =="scissors") return "rock wins" 
        else return "paper wins";
    }

        if(choice1 == "paper"){
        if(choice2 =="scissors") return "scissors wins" 
        else return "paper wins";
    }

        if(choice1 == "scissors"){
        if(choice2 =="paper") return "scissors wins" 
        else return "rock wins";
    }

    compare(userChoice, computerChoice);

}

逻辑和程序流程中的任何错误?

2 个答案:

答案 0 :(得分:1)

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

function compare(choice1, choice2){
    if(choice1 == choice2) return "The result is a tie!";

    if(choice1 == "rock"){
        if(choice2 =="scissors") return "rock wins" 
        else return "paper wins";
    }

    if(choice1 == "paper"){
        if(choice2 =="scissors") return "scissors wins" 
        else return "paper wins";
    }

    if(choice1 == "scissors"){
        if(choice2 =="paper") return "scissors wins" 
        else return "rock wins";
    }
}

compare(userChoice, computerChoice);

你在compare的定义中调用了compare()。

答案 1 :(得分:0)

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

// Sanity check. This is the result if there is no a tie, win or loss.
var userResult = 'You must choose either rock, paper or scissors.';

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

function compare(choice1, choice2) {
    if (choice1 === choice2) {
        userResult = "It is a tie!";
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            userResult = "You win! Rock smashes scissors";
        } else {
            userResult = "You lose! Paper covers rock";
        }
    } else if (choice1 === "paper") {
        if (choice2 === "rock") {
            userResult = "You win! Paper covers rock";
        } else {
            userResult = "You lose! Scissors cut paper";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            userResult = "You win! Scissors cut paper";
        } else {
            userResult = "You lose! Rock smashes paper";
        }
    }
    return userResult;
}

compare(userChoice, computerChoice);

当有人试图扔“马铃薯”或不是摇滚,纸张或剪刀的东西时,我加了一点健全检查。此外,明确赢了或输了,为什么。对不起,我无法抗拒。