javascript - js 文件的 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 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>

谁能解释一下我做错了什么,以及我应该如何改变它?

最佳答案

output 定义的末尾缺少一个右大括号,在 js 的末尾有一个额外的右大括号。在我进行这些更改后,脚本就起作用了。

关于javascript - js 文件的 HTML 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18176438/

相关文章:

javascript - 使用 __MSG_@@extension_id__ 和 jQuery 在 Chrome 扩展弹出窗口中创建链接

javascript - for循环嵌套数组输出

javascript - 水平滚动的 jInvertScroll 方向错误

javascript - "top"相同值的属性显示不同

html - 如何更改CSS中的网格

javascript - 在动态生成的 HTML 上使用插件

javascript - 如何隔离React.js上的点击事件?

javascript - 使用 Html5 getters 和 setters 实现带有显示模块模式的属性需要澄清

HTML5 Canvas 背景图片

移动尺寸的 HTML 背景视频?