У меня есть функция, которая должна определять победителя игры. По какой-то причине он всегда будет печатать первый оператор if, даже если он неверен
Я попытался сделать их все операторы if, меняя скобки, и я разделил все остальные операторы if вместо того, чтобы иметь только 3, и ничего не работает.
function getElem(id) {
return document.getElementById(id);
}
var rounds;
function startGame() {
rounds = getElem("ROUNDS_TO_PLAY");
rounds = parseInt(rounds.value);
document.roundsRemaining = rounds;
getElem("ROUNDS_REMAINING").value = rounds;
}
/* Responds to user choice button click. */
function userChoice(userButton) {
updateStats();
displayUserChoice(userButton);
var compChoice = getComputerChoice();
getElem("COMPUTER_CHOICE_OUTPUT").value = compChoice;
displayComputerChoice(compChoice);
}
function updateStats() {
rounds=document.roundsRemaining;
rounds--;
document.roundsRemaining = rounds;
getElem("ROUNDS_REMAINING").value = document.roundsRemaining;
if (document.roundsRemaining < 1) {
alert("Game over");
getElem("ROCK_CHOICE").disabled=true;
//alert(getWinner(userButton, compChoice));
}
//alert(getWinner());
alert (determineWinner());
}
function getComputerChoice() {
var r = Math.floor(Math.random() * 3)
switch (r) {
case 0: return "ROCK";
case 1: return "PAPER";
case 2: return "SCISSORS";
default: console.log(r + " is not a valid computer choice.");
}
}
function displayUserChoice(userButton) {
var uco = getElem("USER_CHOICE_OUTPUT");
if (userButton == "ROCK") {
uco.value = "ROCK";
} else if (userButton == "PAPER") {
uco.value = "PAPER";
} else if (userButton == "SCISSORS") {
uco.value = "SCISSORS";
} else {
conosole.log(userButton + " is invalid!!");
}
}
function determineWinner(userButton,compChoice) {
if (userButton === compChoice) {
return 'It\'s a tie!';
}
else if (userButton === "ROCK" && compChoice === "PAPER") {
return 'Computer wins!';
}
else if (userButton === 'PAPER' && compChoice === 'SCISSORS') {
return 'Computer wins!';
}
else if (userButton === 'SCISSORS' && compChoice === 'ROCK') {
return 'Computer wins!';
}
else if (userButton === 'PAPER' && compChoice === 'ROCK')
{
return 'You win!';
}
else if (userButton === 'SCISSORS' && compChoice === 'PAPER') {
return 'You win!';
}
else {
return 'You win!';
}
}
// Html отдельный файл
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta charset="UTF-8">
<title>my game</title>
<script src="rsp.js" type="text/javascript"></script>
</head>
<body>
Rounds to Play: <input id="ROUNDS_TO_PLAY"><br>
<input type="button" id="START_GAME" value="Start!" onclick="startGame()">
Rounds Remaining: <input id="ROUNDS_REMAINING"><br><br><br>
<input type="button" id="ROCK_CHOICE" value="Rock" onclick="userChoice('ROCK')">
<input type="button" id="PAPER_CHOICE" value="Paper" onclick="userChoice('PAPER')">
<input type="button" id="SCISSORS_CHOICE" value="Scissors" onclick="userChoice('SCISSORS')">
<br>
User Chose: <input id="USER_CHOICE_OUTPUT" type="text" disabled="true"><br>
Computer Chose: <input id="COMPUTER_CHOICE_OUTPUT" type="text" disabled="true">
</body></html>
В выводе предполагается, что компьютер выиграл, вы выиграли или ничья. Он показывает галстук каждый раз.