2つの関数を持つ外部JavaScriptファイルがあり、ページが読み込まれるたびに両方を呼び出す必要があります。ただし、どちらもしません。tester()関数を呼び出すjsファイルの行をコメント化すると、runGame()が呼び出されますが、tester()がコメント化されていない場合は、どちらも実行されません。ただし、runGame()呼び出しの後にtester()関数呼び出しを配置すると、runGame()は機能しますが、tester()は機能しません。
tester()はそれ自体では実行されないので、おそらくその関数で何か間違ったことがあるでしょう。誰かが私が間違ったことを教えてもらえますか?
html
<html>
<head>
<script type="text/javascript" src="rock_paper_scissors.js">
</script>
</head>
<body>
<p id="game">Test</p>
<br />
<input type="button" value="Play again" onClick="tester()"></input>
</body>
rock_paper_scissors.js
var info = document.getElementById("game");
var tester = function(){
document.getElementById('game').innerHTML = 'hello';
//info.innerHTML("HI");
};
var compare = function(choice1, choice2){
if(choice1 == choice2)
document.write("The result is a tie!");
else if(choice1 == "rock"){
if(choice2 == "scissors")
document.write("You win! Rock wins");
else
document.write("The computer wins! Paper wins");
}
else if(choice1 == "paper"){
if(choice2 == "rock")
document.write("You win! Paper wins");
else
document.write("The computer wins! Scissors wins");
}
else if(choice1 == "scissors"){
if(choice2 == "rock")
document.write("The computer wins! Rock wins");
else
document.write("You win! Scissors wins");
}
else
document.write("You didn't pick a real choice");
};
var runGame = function(){
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
document.write("<p>You picked " + userChoice + "</p>");
document.write("<p>The Computer picked " + computerChoice + "</p>");
compare(userChoice, computerChoice);
};
runGame();
tester();