Rock, paper, scissors there is something wrong with the result!


#1

function getUserChoice(){
var userInput = prompt ('what is your choice ?');
if (userInput==='rock'|| userInput==='paper'||userInput==='scissors'){
return userInput;
} else{
console.log('Error');
}
}
function getComputerChoice(){
var randomNumber = Math.floor(Math.random()*3);
switch(randomNumber){
case 0: return 'paper';
case 1 : return'rock';
case 2 : return 'scissors';
}
}

function determineWiner(getComputerChoice, getUserChoice){

if (getUserChoice==='paper'){
if (getComputerChoice==='scissors'){
return'computer won';
}
else if(getComputerChoice==='rock') {
return 'you won';
}
else{ return'tie1';
}
}
if (getUserChoice==='scissors'){
if (getComputerChoice=== 'rock'){
return'computer won';
}
else if ( getComputerChoice=== 'paper'){
return'you won';
}
else{
return 'tie2';
}
}
if (getUserChoice==='rock'){
if (getComputerChoice==='paper'){
return'computer won';
}
else if ( getComputerChoice==='scissors'){
return 'you won';
}
else{
return'tie3';
}
}
}
function playGame() {
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWiner(userChoice, computerChoice));
}

playGame();


#2

Please explain what you think it should be doing differently, or explain that identifying that is exactly the issue. It isn't clear what you're asking for at the moment.


#3

thanks for the reply, the problem is when the user chooses paper and the computer chooses scissors, the result says 'user won' but the code doesn't say so!!


#4

If the code doesn't say so, then who says it? You seem to be saying that something other than the code is wrong

What should be behaving differently?


#5

look at the first part of (function determineWinner)
if (getUserChoice==='paper'){
if (getComputerChoice==='scissors'){
return'computer won';
}
when you run the code it gives user won


#6

Do you mean that the program's result is that the user wins when the user chooses paper and the computer chooses scissors? You just said that the code doesn't say so, but it must be saying so if that is the result.

You've put numbers on the ties to identify exactly which outcome it was. If that part of the code would have resulted in something else, then perhaps it wasn't executed at all. You could put numbers on the "you won" messages as well for finding out which part was executed


#7

now it works I changed the order of the parameters of determinewinner function it was( getComputerChoice, getUserChoice) i switched them and it worked, i thought the order didnt matter.
thanks for helping.