Rock Paper Scissors

So I have written and re written the code for the project, even made sure my code was exactly the same as in the help tutorial, and it all works perfectly until the determineWinner section. after some digging, I managed to get google’s console to tell me what is wrong, and it looks like it’s a single line (specifically line 29 where it says, return “Computer wins!”; ) messing the whole deal up I’ve checked the other bits of the program and they all seem fine. I did add a userName variable so the computer prints out whoever’s playing the games name, but that seems to be working fine. any jelp would be GREATLY appreciated

let userName = 'Nick'
const getUserChoice = (userInput) => { 
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ) {
  return userInput;
} else {
  console.log('Error!');
}
};
const getComputerChoice = () =>{        
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber){
    case 0:
    return 'rock';
    case 1: 
    return 'paper';
    case 2:
    return 'scissors';
   }
};
const determineWinner = (getUserChoice, getChoice) => {
  if(userChoice === computer) {
   return "it's a tie!";
}
};
if(userChoice === 'rock') {
if(computerChoice === 'paper'){
   return "Computer wins!";       //the issue//
}
else {
"${userName} won!";
}
};
if(userChoice === 'paper') {
if(computerChoice === 'scissors') {
   return "Computer wins!";
}
else {
"${userName} won!";
}
};
if(userChoice === 'scissors') {
if(computerChoice === 'rock') {
   return "Computer wins!";
}
else {
"${userName} won!";
}
};

i recommend you to indent your code properly, so you can see what is nested within the functions and the different conditions.

your function seems to end a bit too early

2 Likes

How so? I guess I’m not sure what you mean

this is currently your function:

const determineWinner = (getUserChoice, getChoice) => {
  if(userChoice === computer) {
   return "it's a tie!";
}
};

the rest of the comparisons is not part of the body of the function

the job of the function is determine the winner, now it only determines a tie

2 Likes

I’ve worked out I had an extra curly here, and it should look like this:

And I know I was short at the end with my curlies, and

if(userChoice === ‘scissors’) {
if(computerChoice === ‘rock’) {
return “Computer wins!”;
}
else {
“${userName} won!”;
}
};

should look like this:

if(userChoice === ‘scissors’) {
if(computerChoice === ‘rock’) {
return “Computer wins!”;
}
else {
“${userName} won!”;
}
}
}

The issue as I understand it from your comment and my own digging, is that originally that extra } I deleted cut the program right there, and adding it to the end opened it to run all the way through, yes? (I know it’s probably a really bad explanation, but I feel like I’m at least close)

1 Like

so you sorted it out?

2 Likes

No, it’s still giving me an error when I try to run it

1 Like

Can you post your full code? I need to be sure of the changes you made

1 Like

Sorry for the delay, here is the code

let userName = 'Nick'
const getUserChoice = (userInput) => { 
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ) {
  return userInput;
} else {
  console.log('Error!');
}
};

const getComputerChoice = () =>{        
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber){
    case 0:
    return 'rock';
    case 1: 
    return 'paper';
    case 2:
    return 'scissors';
   }
}
const determineWinner = (getUserChoice, getChoice) => {
  if(userChoice === computer) {
   return "it's a tie!";
}

if(userChoice === 'rock') {
if(computerChoice === 'paper'){
   return "computer wins!";
}
else {
"${userName} won!";
}
}
if(userChoice === 'paper') {
if(computerChoice === 'scissors') {
   return "Computer wins!";
}
else {
"${userName} won!";
}
}
if(userChoice === 'scissors') {
if(computerChoice === 'rock') {
   return "Computer wins!";
}
else {
"${userName} won!";
}
}
}
console.log(determineWinner('paper', 'paper'));
1 Like

the parameters should be used in the comparison of the determineWinner function.

given you use userChoice and computerChoice in the body of the function, i recommend renaming the parameters

you do understand how parameters and arguments work, right?

1 Like