0

I have one or two questions about how to "randomize" a result on this game when you type the exact same choice.

e.g: I'm calling the function 5 times and if I type different choices (first rock, then paper and then scissors, etc..) it's okay, it throws different random results, but if I type one of them three or five times, like for e.g, paper, it will throw the same result everytime no matter what, and I would like to have different results on it, is this possible without using loops? I'm following TOP courses and I would like not to use loops until I get to that part.

Also, I would like to know if there is some way to print "Machine wins" or "Human wins" after all the attemps, depending on who wins, like a final message for winning or losing all rounds.

Here is the code:

function computerPlay() {
    const choice = ["rock", "paper", "scissors"];
    const choiceRandom = choice[Math.floor(Math.random() * choice.length)];
    return choiceRandom;
}

function game(computerSelection, playerSelection) {

    playerSelection = prompt("Rock, paper or scissors?").toLowerCase();

    if (computerSelection=="rock" && playerSelection=="scissors") {
        console.log("You lose, rock beats scissors");
    } else if (computerSelection=="paper" && playerSelection=="scissors") {
        console.log("You win, scissors beats paper!");
    } else if (computerSelection=="rock" && playerSelection=="paper") {
        console.log("You lose, rock beats paper!");
    } else if (computerSelection=="paper" && playerSelection=="rock") {
        console.log("You lose, paper beats rock!");
    } else if (computerSelection=="scissors" && playerSelection=="rock") {
        console.log("You win, rock beats scissors!");
    } else if (computerSelection=="scissors" && playerSelection=="paper") {
        console.log("You lose, scissors beats paper!");
    } else if (computerSelection===playerSelection) {
        console.log("Draw!");
    }
}

const computerSelection = computerPlay();
let playerSelection

game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);

I'm blank at this, have been trying to figure it out for hours.

Thank you.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • Well, how many times do you call `Math.random`? That’s how many random results you get. – Sebastian Simon Jan 28 '22 at 14:41
  • The variable `computerSelection` won't change unless you change it. – James Jan 28 '22 at 14:44
  • Hmmm, so if computerPlay is stored in computerSelection then I'm calling it 5 times? I am probably wrong... – Quickpois0n Jan 28 '22 at 14:45
  • No, the result from calling computerPlay (once) is stored in computerSelection, and then reused. Consider `game(computerPlay(), playerSelection);` – James Jan 28 '22 at 14:46
  • First, I was using a const instead of a let, that was my bad, second, changing game(computerSelection, playerSelection) to game(computerPlay(), playerSelection) worked like a charm, thank you very much, I really appreciate your help, so instead of calling a const it would be better to call the function itself, right? – Quickpois0n Jan 28 '22 at 15:15
  • You’re not really “calling” a `const`. `const computerSelection = computerPlay();` just means _“call `computerPlay` (once), get its result, store the result in `computerSelection`”_. `computerSelection` is an immutable binding due to the `const` keyword, but more importantly, `computerPlay` is only called _once_. So you only get a single random result. Reusing the variable just retrieves the original result; it does not rerun any functions; programming would be impossibly hard if it did. Each `game(computerPlay(), playerSelection);` calls `computerPlay` (once) and passes its result to `game`. – Sebastian Simon Jan 29 '22 at 02:42
  • See also: [If a variable is defined in terms of another, can it reflect changes in the binding of the other?](/q/42637782/4642212). – Sebastian Simon Jan 29 '22 at 02:46

1 Answers1

0

Regarding you question on randomness, it looks like you are setting your computer's selection to a constant value passing it to your game function - meaning it will always be the same!

If you place both the computerSelection and playerSelection in the game function the computerSelection will call the computerPlay function each time you start a game. As for the player selection, it belongs in the function because that is as far the scope needs to extend.

Finally, you can wrap the inside of you game function in a for loop and declare two variables machineWins and playerWins. Increment the wins for each. Then at the end of the function you can have an if else statement that will log your message based on who has the most wins.

game() {
  let machineWins = 0;
  let playerWins = 0;
  for (let gamesPlayed = 0; gamesPlayed < 5; gamesPlayed++) {
   // game logic, if computer wins machineWins++, else playerWins++
  }
  if (machineWins > playerWins) {
   // log machine wins
  } else {
   // log player wins
  }
}
zemaj
  • 330
  • 1
  • 7
  • I just answered the other guy who helped me, you are right, I was using a constant instead of a variable, changing it to game(computerPlay(), playerSelection) solved the problem but I don't know if I am right? About using loops, is there any way to set a final winners message after all rounds but without using loops? I don't want to use them just yet until I learn more about it! Thank you. – Quickpois0n Jan 28 '22 at 15:17
  • Yes there is a way, you could set the variables tracking wins outside the function and still update them, then after all your function codes use the if else statement to print a message. Loops are not that difficult though, don't shy away from them. They are a critical part of programming. – zemaj Jan 28 '22 at 15:22
  • Uhm, I know but Im taking things slowly until I get to that lesson, how do you set a variable to track down the score? – Quickpois0n Jan 28 '22 at 15:48
  • So you want to create two variables right, one to track machineWins the other to track playerWins. You would want to declare these variables outside the game function and initialize them with a value of 0, since no games have been played when the program starts. Then inside your game function, and inside each of your if/else statements you'll want to increment the value of machineWins or playerWins depending on who won that game. At the end of you current program you can add another if/else block where you compare playerWins to machineWins and console log who won. – zemaj Jan 28 '22 at 17:57
  • Thank you very much, since I am only 600 characters I just copy my codepen.io, this is the result of it, I don't know if I'm missing something but so far it have worked for me: https://codepen.io/khrystoff/pen/YzEXowY Again, thanks! – Quickpois0n Jan 28 '22 at 19:54