此时此刻,我正在尝试学习javascript,但它让我很难过。
我写了一个小石头剪刀脚本,它应该给用户一些输出,但我似乎无法真正得到那个输出。
js脚本是这样的:
var computerChoiceLine = 0
var tieResult = "The result is a tie!";
var winResult = "You won!";
var lossResult = "You lost..."
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
// convert computerChoice into text
if (computerChoice <= 0.34) {
computerChoiceLine = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
// convert userChoice into number
if (userChoice == "rock") {
userChoiceNumber = 0;
} else if(userChoice == "paper") {
userChoiceNumber = 0.50;
} else if(userChoice == "scissors"){
userChoiceNumber = 1;
} else {
userChoiceNumber = 100;
}
var output = function(choice1, choice2){
document.write("You chose " + choice1)
document.write("\n The computer chose " + choice2)
compare(userChoiceNumber , computerChoice)
var compare = function(choice1 , choice2){
if(choice1 == choice2){
document.write(tieResult);
}
if(choice1 < choice2){
document.write(lossResult);
}
if(choice1 > choice2){
document.write(winResult);
}
};
if(userChoice == 100){
document.write("Please type rock, paper or scissors")
} else {
output(userChoice , computerChoiceLine);
};
}
我在HTML中这样称呼它:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="RockPaperScissors.js"></script>
</body>
</html>
任何人都可以解释我做错了什么,以及我该如何改变它?
答案 0 :(得分:1)
你在output
的定义末尾有一个缺失的近支撑,并且在你的js末尾有一个额外的近支撑。在我做了这些更改后,脚本运行了。