js文件的HTML输出
HTML output from a js file
此时此刻,我正在尝试学习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的末尾有一个额外的右大括号。在我做了这些修改之后,剧本就起作用了。
相关文章:
- 使用Jade输出HTML(在ng个重复内)
- 输出 html 表单数据
- 如何通过 AngularJS 模板输出 html
- Ajax 输出 html 注释
- 显示查询输出 HTML 中的图像
- HTML 代码不适用于 jquery 调用,而是输出 HTML 代码
- Html.ActionLink onclick JavaScript 函数未被调用,并且函数名称在输出 HTML 中已损
- Javascript -使用字符串输出HTML
- 如何正确使用jQuery从php输出html字符串
- 为什么.html()不输出html标签
- 如何获得一个变量's输出html值
- Twitter-text.js输出html作为纯文本
- 多行文本文件输出html
- 如何使用XSLT匹配元素和输出HTML
- 输出HTML代码
- 输出HTML时出现意外的令牌错误
- 用JavaScript输出HTML实体
- 在rjerb模板中输出HTML
- 从angularjs过滤器输出HTML,无需ng绑定HTML
- 获取实际输出HTML的函数