The slight change in my Rock, Paper and Scissors game code that makes everything go crazy. This is driving me nuts, please help


#1

I have been trying to figure this out for hours. I think it’s absolutely vital to know what’s going on here, but the problem is that I see absolutely nothing wrong with the code at hand.

So let me explain. I was creating the Rock, Paper, Scissors game and I actually took a detour and created it in a different way than what the suggestions stated. The following code is that game and it works perfectly, you can test it out yourself.

So what’s the problem? The moment I define the randomNumber variable within the first function (yet still declare it outside the function as a global variable), for some reason, the whole program goes nuts, the random numbers don’t match their outputs as rock/paper/scissors and even the next function cannot figure out who won based on this data.
I have absolutely no idea why this happens and to me it makes zero sense. I know that my original code works, but I really need to know why the other code doesn’t in order to understand what’s going on and not make the same mistake elsewhere. I assume I must be missing something.

The code that WORKS:


var userChoice = prompt("please enter one of the following: rock, paper or scissors", "");
var randomNumber =  Math.floor(Math.random()*3);

function computerChoice()
{
    if (randomNumber === 0) {
    return "rock";} 
    else if (randomNumber === 1) {
    return "paper";}
    else {
      return "scissors";}
}

computerChoice();


console.log("The random number was: " + randomNumber);
console.log("The computer's choice was: " + computerChoice());

function whoWon()
{
    if (userChoice === computerChoice()) {
return "It's a tie!";} 

else if (userChoice === "rock") {

    if (computerChoice() === "paper") { 
      return "The computer won.";}
    
    else  {
      return "The user won.";}
                                }

    
   else if (userChoice === "paper"){
    if (computerChoice() === "scissors") {
      return "The computer won.";
    }
    else { 
    return "You won.";}
                             }
    
   else if (userChoice === "scissors"){
      if(computerChoice() === "rock") {
        return "Computer won.";}
      
      else {
        return "You won.";}
                                       }
}

whoWon();
console.log(whoWon());

The code that does NOT work:


var userChoice = prompt("please enter one of the following: rock, paper or scissors", "");
var randomNumber;

function computerChoice()
{
    randomNumber = Math.floor(Math.random()*3);

    if (randomNumber === 0) {
    return "rock";} 
    else if (randomNumber === 1) {
    return "paper";}
    else {
      return "scissors";}
}

computerChoice();


console.log("The random number was: " + randomNumber);
console.log("The computer's choice was: " + computerChoice());

function whoWon()
{
    if (userChoice === computerChoice()) {
return "It's a tie!";} 

else if (userChoice === "rock") {

    if (computerChoice() === "paper") { 
      return "The computer won.";}
    
    else  {
      return "The user won.";}
                                }

    
   else if (userChoice === "paper"){
    if (computerChoice() === "scissors") {
      return "The computer won.";
    }
    else { 
    return "You won.";}
                             }
    
   else if (userChoice === "scissors"){
      if(computerChoice() === "rock") {
        return "Computer won.";}
      
      else {
        return "You won.";}
                                       }
}

whoWon();
console.log(whoWon());

What do I mean? I mean that the outputs make no sense. Run the program a few times and see for yourself.

Let’s say I always pick “rock” (write in “rock” in the pop-up, obviously if you want you can also change the first variable into whatever you want directly (rock/paper/scissors), bypassing the pop-up, which is the user input). Here are some results from the console:

*note also how 0=rock, 1=paper and 2=scissors (or so it should be based on the code)

In the code that works this is perfectly executed:

The random number was: 2
The computer’s choice was: scissors
The user won.

The random number was: 0
The computer’s choice was: rock
It’s a tie!

The random number was: 1
The computer’s choice was: paper
The computer won.

In the code that doesn’t work (note that even here sometimes a result crops up that makes sense, but run the program a few times and you will see these totally nonsensical results):

The random number was: 2
The computer’s choice was: rock
The user won.

The random number was: 0
The computer’s choice was: scissors
The user won.

Can someone please explain what’s going on here? It’s driving me insane. Thanks in advance :slight_smile:


#2

each time you call the computerChoice function, a new random number is generated and the computer picks a different option (rock, paper or scissors)

so here:


computerChoice();


console.log("The random number was: " + randomNumber);
console.log("The computer's choice was: " + computerChoice());

the first time you call the function (computerChoice();), the output could be rock.

then you call the function again:

console.log("The computer's choice was: " + computerChoice());

this time the output might be paper

you should just call the function once, to give the computer one option/choice, and stick with that option through the rest of the program.

the reason the first code works is because you generate the random number once, and user the same number over and over again, it works, its not very elegant if i am honest.


#3

Hey, thank you so much! I truly appreciate the reply.

Yes, I see it now. I call the function numerous times afterwards as well. I didn’t know you were repeatedly calling a function even when it is located in console.log and within another function. Well I have been only doing this for about a week now so I have plenty to learn :slight_smile:

With this new info, I found a way around it by parsing the function output into a variable, which made it work as well as the other code:


var userChoice = prompt("please enter one of the following: rock, paper or scissors", "");
var randomNumber;

function computerChoice()
{

randomNumber = Math.floor(Math.random()*3);
    if (randomNumber === 0) {
    return "rock";} 
    else if (randomNumber === 1) {
    return "paper";}
    else {
      return "scissors";}
}

var computerChoice = computerChoice();

function whoWon()
{
    if (userChoice === computerChoice) {
return "It's a tie!";} 

else if (userChoice === "rock") {

    if (computerChoice === "paper") { 
      return "The computer won.";}
    
    else  {
      return "The user won.";}
                                }

    
   else if (userChoice === "paper"){
    if (computerChoice === "scissors") {
      return "The computer won.";
    }
    else { 
    return "You won.";}
                             }
    
   else if (userChoice === "scissors"){
      if(computerChoice === "rock") {
        return "Computer won.";}
      
      else {
        return "You won.";}
                                       }
}


whoWon();

console.log("The computer's choice was: " + computerChoice);
console.log("The random number was: " + randomNumber);
console.log(whoWon());

Still though, I assume it could be done better. Any other advice that would make it more elegant would be appreciated. No need to go into detail if you don’t have the time, but any pointer on how to make this better would be helpful. Thanks in advance.


#4

this is already a lot better, but it still could be better.

using global variable within a function has drawback (link), so i would remove this line for example:

var randomNumber;

then here:

whoWon();

console.log("The computer's choice was: " + computerChoice);
console.log("The random number was: " + randomNumber);
console.log(whoWon());

why do you call whoWon twice?

i would make another function, for example playGame or something, inside this function:

var userChoice = prompt("please enter one of the following: rock, paper or scissors", "");
var computerChoice = computerChoice();
console.log("The computer's choice was: " + computerChoice);

then utilize parameters and arguments properly for whoWon function, so there are no global variable left.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.