Javascript未运行-无错误消息
我已经准备好了这个石头剪刀游戏。但是,Javascript没有运行,我没有收到任何错误。有什么建议吗Javascript未运行-无错误消息,javascript,Javascript,我已经准备好了这个石头剪刀游戏。但是,Javascript没有运行,我没有收到任何错误。有什么建议吗 function play(humanScore) { var computerScore = getcomputerScore(); if (humanScore == "rock") { if (computerScore == "rock") { } else if (computerScore == "scissors") {
function play(humanScore) {
var computerScore = getcomputerScore();
if (humanScore == "rock") {
if (computerScore == "rock") {
} else if (computerScore == "scissors") {
human++
} else if (computerScore == "paper") {
computer++
}
} else if (humanScore == "scissors") {
if (computerScore == "scissors") {
} else if (computerScore == "paper") {
human++
} else if (computerScore == "rock") {
computer++
}
} else if (humanScore == "paper") {
if (computerScore == "paper") {
} else if (computerScore == "scissors") {
computer++
} else if (computerScore == "rock") {
human++
}
}
}
function getcomputerScore() {
var randomplay = ["rock", "paper", "scissors"];
var play = randomplay[Math.floor(Math.random() * myArray.length)];
return play
}
这是设置humanScore的代码:
属性的名称是onclick,而不是onclick;注意小写的c
正如@RobG所指出的,至少还有一个错误myArray.length,但这将使它们实际抛出。可能更适合于代码审查部分,但下面是
function play(humanScore) {
var computerScore = getcomputerScore();
变量humanScore和computerScore实际上不是分数,它们是玩家选择玩的符号,因此变量可能比humanChoice和computerChoice更好。这也意味着全球人类和计算机可以更好地命名为humanScore和computerScore
与其留下一个空白块,不如插入一条评论说没有调整。最好先测试等价的选择,然后剩下二进制选择,这样您就可以执行以下操作:
var humanScore = 0;
var computerScore = 0;
function play(humanChoice) {
// IE tends to play with capitalisation of values
humanChoice = humanChoice.toLowerCase();
var computerChoice = getComputerChoice();
// If same choice, no change of score
if (computerChoice == humanChoice) {
return;
}
// Only binary win/lose choices left
if (humanChoice == 'rock') {
computerChoice == 'paper'? ++computerScore : ++humanScore;
} else if (humanChoice == 'paper') {
computerChoice == 'scissors'? ++computerScore : ++humanScore;
} else if (humanChoice == 'scissors') {
computerChoice == 'rock'? ++computerScore : ++humanScore;
}
}
此函数中也存在错误:
function getComputerChoice() {
var choices = ['rock','paper','scissors'];
return choices[Math.random()*choices.length | 0];
}
最后,在添加侦听器之前,确保按钮位于页面中,并确保属性名称的大小写正确。这对HTML属性不重要,但对javascript属性名称重要:
window.onload = function() {
document.getElementById("rock").onclick = pickRock;
document.getElementById("scissors").onclick = pickScissors;
document.getElementById("paper").onclick = pickPaper;
}
请将其放入一个文件中,因为您尚未提供HTML,因此我们无法确定您是否正在执行调用这些函数的操作。在getcomputerScore函数中,您执行myArray.length,这是从何而来的?我想您的意思是使用randomplay.length.HTML属性名称不区分大小写。onclick可能是为了与XHTML兼容而首选的,但onclick会起作用。@Barmar:我说的是属性;由于某种原因,OP删除了带有设置代码的注释,可能是因为它没有以格式化的方式显示?。我把它放回问题里了。哦,对不起,我没看到。
function getComputerChoice() {
var choices = ['rock','paper','scissors'];
return choices[Math.random()*choices.length | 0];
}
window.onload = function() {
document.getElementById("rock").onclick = pickRock;
document.getElementById("scissors").onclick = pickScissors;
document.getElementById("paper").onclick = pickPaper;
}