The rock paper scissors project

Hi my roc paper scissors project is not working as expected. when i run function determineWinner i dont get the right result instead it just prints the first result witch is a tie and if i remove that then it says that i have wone which is the 4 line.

Hi @tomskalnins420024382! Welcome to the forums :grinning:

Could you post your code here(be sure to use the </> button so it gets formatted) so it’s easier to help you find your problem?

3 Likes

<>const getUserChoice = userInput =>{
switch (userInput){
case 1:
return ‘rock’;
case 2:
return ‘paper’;
case 3:
return ‘scissors’;
default :
return ‘error’
}

};

const getComputerChoice = () =>{
const randomNumber= Math.floor(Math.random()*3)
switch (randomNumber)
{
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scisors’;
default :
return ‘error’;
}
};

const determineWinner = (userChoice,computerChoice) =>{
if(userChoice === computerChoice){
return ‘This Mach is a tie!’
}if(userChoice === ‘rock’){
if(computerChoice ===‘paper’)
return ‘Sorry computer won.’
}else {
return ‘congradulations you won!’
}
if(userChoice === ‘scissors’){
if(computerChoice ===‘rock’)
return ‘Sorry computer won.’
}
else {
return ‘congradulations you won!’
}
if(userChoice === ‘paper’){
if(computerChoice ===‘scissors’)
return ‘Sorry computer won.’
}else {
return ‘congradulations you won!’
}

};

const playGame = () => {
const Userchoice = getUserChoice(‘paper’);
const Computerchoice = getComputerChoice();
console.log('You threw: '+ Userchoice);
console.log('the computer threw: '+ Computerchoice);
console.log(determineWinner(Userchoice,Computerchoice));
}

playGame()

</>

I think there’s a problem with your conditions.

First of all, your nested if (where you check if computerChoice equals “paper”) is missing a pair of {}. It should have those {} just like the first if statement does.

Now, after you fix that…

I’m gonna break down what the code above is doing:

  1. Check if the userChoice is ‘rock’
  2. If it is, now check is the computerChoice is ‘paper’
  3. If both of those conditions are met, then the computer won.
  4. If the userChoice is not ‘rock’, then the user won!

Doesn’t seem right, does it? Your else statement actually…let’s say, belongs to the if that’s checking the value of userChoice. So basically you’re just saying, “if the userChoice is ‘rock’ then go into another condition, but it it’s not ‘rock’, then the user won.

The right way to do this should be like this:

  1. Check if the userChoice is ‘rock’
  2. If it is, now check is the computerChoice is ‘paper’
  3. If both of those conditions are met, then the computer won.
  4. If the userChoice is ‘rock’, but the computerChoice is anything else than ‘scissors’, then the user won!
I hope that gives you an idea of what you have to change in your code, but if it truly doesn’t...

Your code should look kinda like this:


if(userChoice === ‘rock’){
if(computerChoice ===‘paper’){
return ‘Sorry computer won.’
} else {
return ‘congradulations you won!’
}

Also you have a typo in your getComputerChoice() function. You’re returning ‘scisors’ instead of ‘scissors’.