Javascript没有运行 - 没有错误消息

时间:2014-04-17 00:56:25

标签: javascript

我已经设置了这个摇滚,剪刀游戏。但是,Javascript没有运行,我没有收到任何错误。有什么建议吗?

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

var human = 0;
var computer = 0;

document.getElementById("rock").onClick = pickRock;
document.getElementById("scissors").onClick = pickScissors;
document.getElementById("paper").onClick = pickPaper;

function pickRock() {
    play("rock");
}

function pickScissors() {
    play("scissors");
}

function pickPaper() {
    play("paper");
}

2 个答案:

答案 0 :(得分:0)

该属性的名称为onclick,而不是onClick;请注意小写c

至少还有一个其他错误(myArray.length,正如@RobG指出的那样),但这会让他们真正抛出。

答案 1 :(得分:0)

可能更适合代码审查部分,但这里......

function play(humanScore) {
    var computerScore = getcomputerScore();

变量 humanScore computerScore 实际上并不是分数,它们是玩家选择玩的符号,因此变量可能会更好 humanChoice computerChoice 。这也意味着全局 human 计算机可以更好地命名为 humanScore computerScore

if (humanScore == "rock") {
    if (computerScore == "rock") {
    } else if (computerScore == "scissors") {

不要留空格块,最好插入评论说“无调整”。最好先预测相同的选择,然后选择二元选项,这样你就可以做到:

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;
}