0

I'm brand new to learning to code and can't figure out why my below function is not working as intended. I've searched through many other questions/answers but can't seem to find a solution that works.

The goal of the below code is to have the computer choose from the array (rock, paper, scissors) and then to prompt the user to input their choice. To see if what I had so far worked, without creating the full if, else function, I simplified it for now so that the user can only win in one scenario.

Even if the computer chooses scissors and I input rock as the user which is supposed to be the winning combination - it always gives the "Hmm?" message....why?!

I thought perhaps it's because compsChoice is a number, but I can't seem to figure out how to make it a string. I tried the String() and .toString methods but they did not work - maybe I wrote the syntax wrong...

<script>

function computerPlay() {

const gameInputs = ['rock', 'paper', 'scissors'];
let compsChoice = [Math.floor(Math.random() * gameInputs.length)];

console.log(gameInputs[compsChoice]);

}

computerPlay();


function playRound (compsChoice, userSelection) {
userSelection = prompt ("Rock, Paper or Scissors?"); 

// only alerts HMM no matter what...
if (userSelection === 'rock' && compsChoice === 'scissors') {
alert ('You WIN!');
} else {
alert ('HMM?');
}
}
playRound();
</script>
Anna-Delta
  • 15
  • 5
  • What have you tried to resolve the problem? Where are you stuck? As modern browsers contain excellent debuggers for JS code, have you tried any of them to spot the problem? – Nico Haase Jun 23 '21 at 11:50
  • For example, have you checked what these variables contain? You forgot to add them to the call of `playRound()` – Nico Haase Jun 23 '21 at 11:51
  • Nico - I'm not sure i understand - which variables? The ones I have as paramaters for playRound? Do I even need to have paramaters at all or can I just put "function playROund (); ? – Anna-Delta Jun 23 '21 at 20:21
  • The function `playRound` has two arguments, `compsChoice` and `userSelection`. But you call `playRound` without arguments – Nico Haase Jun 24 '21 at 05:58

0 Answers0