1

I'm trying to figure out how to play out a single round of the game but when I run this function it does yield the correct results;

//AI Random Pick function

var AIpick = ['Rock', 'Paper', 'Scissors']; 
    
function getComputerChoice() {
    return  AIpick[Math.floor((Math.random()*AIpick.length))]; 
}

console.log(getComputerChoice(AIpick));

Random pick function above works as intended.

//Single Round Of the Game
function round(playerSelection, computerSelection) {
    if (playerSelection == "Rock" && computerSelection == "Paper") {
        return "You lose! Paper beats Rock!";
    } else if (playerSelection == "Paper" && computerSelection == "Rock") {
        return "You won! Paper beats Rock!";
    } else if (playerSelection == "Rock" && computerSelection == "Rock") {
        return "Tie! Unga Bunga! Makes a nice sound tho!";
    }

}

const playerSelection = "Rock";
const computerSelection = getComputerChoice();

console.log(round(playerSelection, computerSelection))

Something is problematic with the function above. It gives me the tie option when the computerSelection = Paper and 'you lose' option when the 'computerSelection = Rock'

Torge Rosendahl
  • 482
  • 6
  • 17
  • 3
    You're not logging `computerSelection`, you're just logging another random pick. How do you know that `computerSelection` is `"Paper"`? – kelsny Mar 28 '23 at 21:05
  • 3
    You are not handling every possible combination. For example, what happens when the computer has `"Scissors"`? – 001 Mar 28 '23 at 21:07
  • It works perfect for me if I hardcode the values for `computerSelection`. – Ignatius Reilly Mar 28 '23 at 21:07
  • Duplicate of the whole game code : https://stackoverflow.com/questions/75624474/how-to-make-the-computer-pick-another-random-selection-after-the-player-chooses/75626686#75626686 – tatactic Mar 28 '23 at 21:30

1 Answers1

0

Your logging at two different times.

You are logging a random value in the top code snippet. Then you play with another random value in the second code snippet.

To log what you are playing currently, remove the print in the upper code snippet and change the bottom one into this:

//Single Round Of the Game
function round(playerSelection, computerSelection) {
    if (playerSelection == "Rock" && computerSelection == "Paper") {
        return "You lose! Paper beats Rock!";
    } else if (playerSelection == "Paper" && computerSelection == "Rock") {
        return "You won! Paper beats Rock!";
    } else if (playerSelection == "Rock" && computerSelection == "Rock") {
        return "Tie! Unga Bunga! Makes a nice sound tho!";
    } //TODO remainder of possible combinations

}

const playerSelection = "Rock";
const computerSelection = getComputerChoice();

console.log(computerSelection) // <- THIS LINE IS NEW
console.log(round(playerSelection, computerSelection))
Torge Rosendahl
  • 482
  • 6
  • 17