Javascript js文件的HTML输出
现在,我正在努力学习javascript,但这给了我一段艰难的时间 我已经写了一个小的石头剪刀脚本,它应该给用户一些输出,但我似乎无法实际获得输出 js脚本如下所示:Javascript js文件的HTML输出,javascript,html,Javascript,Html,现在,我正在努力学习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 r
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);
};
}
var computerChoiceLine=0
var tiesult=“结果是平局!”;
var winResult=“你赢了!”;
var lossResult=“您丢失了…”
var userChoice=prompt(“您选择石头、布还是剪刀?”);
var computerChoice=Math.random();
//将计算机选项转换为文本
如果(computerChoice您在输出的定义末尾缺少一个右大括号,在js的末尾有一个额外的右大括号。在我做了这些更改之后,脚本开始工作。您在输出的定义末尾缺少一个右大括号,在js的末尾有一个额外的右大括号。在我取消这些更改脚本工作。您会得到什么错误?在实际定义它之前,您调用compare
。您是否错过了tiesult
的输出?正如我所见,在调用它之前,我已经定义了compare。我没有得到任何错误,我只得到一个空白网页,它会发出提示,但页面仍然是空白的h@skishore的答案是解决了这个问题。您得到了什么错误?您在实际定义它之前调用了compare
。您是否错过了tiesult
的输出?正如我看到的,我在调用它之前定义了compare。我没有得到任何错误,我只得到了一个空白网页,它会发出提示,但页面永远不会保持空白记住,有了@skishore的答案,这个问题已经解决了很多,这让它工作了!测试它,让我意识到我的比较函数毫无意义。感谢很多,这让它工作了!测试它,让我意识到我的比较函数毫无意义
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="RockPaperScissors.js"></script>
</body>
</html>