저는 "Rock Paper Scissors"게임을 통해 JavaScript의 기본 사항을 이해하고 있는지 확인하고 있습니다. fight()
의 30 번째 줄부터 71 번째 줄까지 if 문을 사용할 때까지는 모두 잘 작동합니다. 응답 변수를 조정하여 "if'ing"이 될 수 있음을 나타냅니다. 전반적인 "if"에서 모든 하위 "if"의 기본값을 반환하는 것으로 보입니다. 어쩌면 보조 변수에 잘못된 변수를 사용하고 있습니까?스위치 문을 잘못 포맷합니까?
radioGroup.html이
<script type = "text/javascript">
//<![CDATA[
// from radioGroup.html
function fight(){
var randomChoice = Math.random();
var threeChoice = Math.floor (randomChoice * 3);
var weapon = document.getElementsByName("weapon");
for (i = 0; i < weapon.length; i++){
currentWeapon = weapon[i];
if (currentWeapon.checked){
var selectedWeapon = currentWeapon.value;
} // end if
} // end for
var cpuChoice = new Array("Paper", "Rock", "Scissors")
var reply = "xxx"
if (threeChoice === 0) {
if (weapon === "Paper") {
reply = "11";
}
else if (weapon === "Rock") {
reply = "12";
}
else if (weapon === "Scissors") {
reply = "13";
}
else {
reply = "what1";
}
}else if (threeChoice === 1) {
if (weapon === "Paper") {
reply = "21";
}
else if (weapon === "Rock") {
reply = "22";
}
else if (weapon === "Scissors") {
reply = "23";
}
else {
reply = "what2";
}
}else if (threeChoice === 2) {
if (weapon === "Paper") {
reply = "31";
}
else if (weapon === "Rock") {
reply = "32";
}
else if (weapon === "Scissors") {
reply = "33";
}
else {
reply = "what3";
}
}else {
reply = "hay now?";
}
var output = document.getElementById("output");
var response = "<h2>You have chosen ";
response += selectedWeapon + "<h2>The CPU has chosen ";
"<\/h2> \n";
response += cpuChoice[threeChoice] + "<\/h2> \n";
response += reply + "<\/h2> \n";
output.innerHTML = response;
} // end function
//]]>
</script>
</head>
<body>
<h1>Choose!</h1>
<form action = "">
<fieldset>
<input type = "radio"
name = "weapon"
id = "radPaper"
value = "Paper"
checked = "checked" />
<label for = "radPaper">Paper</label>
<input type = "radio"
name = "weapon"
id = "radRock"
value = "Rock" />
<label for = "radRock">Rock</label>
<input type = "radio"
name = "weapon"
id = "radScissors"
value = "Scissors" />
<label for = "radScissors">Scissors</label>
<button type = "button"
onclick = "fight()">
fight the dragon
</button>
</fieldset>
</form>
<div id = "output">
</div>
</body>
</html>
달려 있습니다. 비교 변수가 항상 같습니까? "(weapon ==="Paper ") && (threeChoice === 0)"? == "(weapon ==== 'Paper') && (threeChoice === 0)"{/ * do this/*} –