我是Javascript的新手,我正在创建一个RPS游戏。出于某种原因,当我弹奏剪刀按钮时,它只是记录点。我很确定我使用的东西不应该关闭,但我无法找到。任何帮助,将不胜感激。
这是我的javascript代码:
var bot = 0;
var you = 0;
document.getElementById('rock').onclick = playRock;
function playRock() {
var humanChoice = 'rock';
var compChoice = cChoice();
console.log(compChoice);
compare(humanChoice, computerChoice);
};
function compare(humanChoice2, compChoice2) {
if (humanChoice2 == compChoice2) {
console.log("tie")
} else if (humanChoice2 == "rock"){
if (compChoice2 == 'scissors') {
you = you + 1;
document.getElementById("humanScore").innerHTML = you;
}
else {
bot = bot + 1;
document.getElementById("computerScore").innerHTML = bot;
}
}
}
function cChoice() {
computerChoice = Math.random();
if (computerChoice <= .33) {
computerChoice = 'rock';
} else if (computerChoice <= .66) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
return computerChoice;
}
document.getElementById('paper').onclick = playPaper;
function playPaper() {
var humanChoice = 'paper';
var compChoice = cChoice();
console.log(compChoice);
compare(humanChoice, computerChoice);
};
function compare(humanChoice2, compChoice2) {
if (humanChoice2 == compChoice2) {
console.log("tie")
} else if (humanChoice2 == "paper"){
if (compChoice2 == 'rock') {
you = you + 1;
document.getElementById("humanScore").innerHTML = you;
}
else {
bot = bot + 1;
document.getElementById("computerScore").innerHTML = bot;
}
}
}
function cChoice() {
computerChoice = Math.random();
if (computerChoice <= .33) {
computerChoice = 'rock';
} else if (computerChoice <= .66) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
return computerChoice;
}
document.getElementById('scissors').onclick = playScissors;
function playScissors() {
var humanChoice = 'scissors';
var compChoice = cChoice();
console.log(compChoice);
compare(humanChoice, computerChoice);
};
function compare(humanChoice2, compChoice2) {
if (humanChoice2 == compChoice2) {
console.log('tie')
} else if (humanChoice2 == 'scissors'){
if (compChoice2 == 'paper') {
you = you + 1;
document.getElementById('humanScore').innerHTML = you;
}
else {
bot = bot + 1;
document.getElementById('computerScore').innerHTML = bot;
}
}
}
function cChoice() {
computerChoice = Math.random();
if (computerChoice <= .33) {
computerChoice = 'rock';
} else if (computerChoice <= .66) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
return computerChoice;
}
答案 0 :(得分:1)
你怎么能有三个具有相同签名的功能? 比较(humanChoice2,compChoice2)
仅使用最后一个定义。尝试以不同方式命名每个函数。
function a()
{
document.getElementById("demo").innerHTML = "1";
}
function a()
{
document.getElementById("demo").innerHTML = "2";
}
function a()
{
document.getElementById("demo").innerHTML = "3";
}
a();
这会将demoHT的innerHTML设置为3