Josef Molz Josef Molz - 16 days ago 5
HTML Question

Rock-Scissors-Paper game - Code does not work

My rock-scissors-paper game is not working.

I am a JavaScript beginner and need your help.

Here is my code:



function user(x) {
var element_user = document.getElementById('user');
return element_user.innerHTML = x;
}

function enemy() {
random_number = Math.ceil(Math.random() * 3);

if (random_number == 1) {
random_number = "Stein";
} else if (random_number == 2) {
random_number = "Schere";
} else {
random_number = "Papier";
}
var element_enemy = document.getElementById('enemy');
return element_enemy.innerHTML = random_number;
}

function winner() {
if (user() === 'Stein' && random_number === 'Schere') {
return "User hat gewonnen!";
} else if (user() === 'Stein' && random_number === 'Papier') {
return "Enemy hat gewonnen!";
} else if (user() === 'Schere' && random_number === 'Stein') {
return "Enemy hat gewonnen!";
} else if (user() === 'Schere' && random_number === 'Papier') {
return "User hat gewonnen!";
} else if (user() === 'Papier' && random_number === 'Stein') {
return "User hat gewonnen!";
} else if (user() === 'Papier' && random_number === 'Schere') {
return "Enemy hat gewonnen!";
} else {
return "Unentschieden!";
}
var winner_element = document.getElementById('winner');
winner_element.innerHTML = winner();
}

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>S+S+P</title>
</head>
<body>

<p id="user"></p>
<p id="enemy"></p>

<button onclick="user('Stein'), enemy();">Stein</button>
<button onclick="user('Schere'), enemy();">Schere</button>
<button onclick="user('Papier'), enemy();">Papier</button>

<p id="winner"></p>

<button onclick="winner();">Gewinner</button>

<script src="game.js"></script>
</body>
</html>






  1. User can click either on rock, scissors or paper;

  2. The result of the user is shown above + random choice by my JS program;

  3. When you click 'Gewinner', it should show who wins the game.



I'm stuck on #3. The winner doesn't show up.

Thanks,

Josef

Answer

sorry if i change the words to English. I cant understand german?

var element_user = document.getElementById('user');
var element_enemy = document.getElementById('enemy');
var winner_element = document.getElementById('winner');

function user(x) {
  return element_user.innerHTML = x;
}

function enemy() {
  random_number = Math.ceil(Math.random() * 3);

  if (random_number == 1) {
    random_number = "Stein";
  } else if (random_number == 2) {
    random_number = "Schere";
  } else {
    random_number = "Papier";
  }
  return element_enemy.innerHTML = random_number;
}    

function checkWinner(user, enemy){
  if (user === enemy) {
    return "Its a tie";
  }

  if(user === "Stein"){
      if(enemy === "Papier"){
        return "Enemy wins";
      }
      return "User wins";
  }

  if(user === "Papier"){
      if(enemy === "Stein"){
          return "User wins";
      }
      return "Enemy wins";
  }

  if(user === "Schere"){
     if(enemy === "Stein"){
        return "Enemy wins";
     }

     return "User wins";
  }
}

function winner() {
  var user = element_user.innerHTML;
  var enemy = element_enemy.innerHTML;
  winner_element.innerHTML = checkWinner(user, enemy);
}
 <p id="user"></p>  
  <p id="enemy"></p>
  
  <button onclick="user('Stein'), enemy();">Stein</button>
  <button onclick="user('Schere'), enemy();">Schere</button>
  <button onclick="user('Papier'), enemy();">Papier</button>
  
  <p id="winner"></p>
  
  <button onclick="winner();">Gewinner</button>