Stuck on 10/14


#1

So I’m a little stuck here and would LOVE some help!

I’m a little confused, because when I run the code, it will return my choice, the computers choice, but will always return the “its a tie” result regardless of the choices,
and if i comment this section out and run the code again, then I get an error message after the two choices have been returned, as if the rest of the IF statements are wrong.

I have used all the hints and previous forum posts but am still none the wiser!

TIA :slight_smile: x

const getUserChoice = userInput => {
  
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
  return userInput;
  } else {
    console.log("This isn't a gun fight! choose rock, paper or scissors!")
  }
}; /*function close */

const getComputerChoice = () => {
  
const randomNumber = Math.floor(Math.random() * 3 );
  
  if (randomNumber === 0) {
    return 'rock';
  
  } else if (randomNumber === 1) {
    return 'paper';
    
  } else {
    return 'scissors';
  }
  	
  }; /* function close */

const determineWinner = (userChoice, computerChoice) => {
  
   if (userChoice === computerChoice) {
    return 'we have a tie!';
  };
  
 if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'the machines win!';
      } else {
        return "victory for sapiens!";
      }
}; /* if statement close */
  
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return('Computer wins!')
  } else {
      return "Player 1,000,000 wins!";
    }
  }; /* if statement close */

if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return "The computers are taking over... light the beacons!!";
  } else {
    return "We did it!";
  }
  }; /* if statement close */
  
 }; /* function closes here */

console.log(getUserChoice('rock'));
console.log(getComputerChoice());
console.log(determineWinner());


#2

Just inside the function, log out the two variables…

console.log(userChoice, computerChoice)

What do you get?

undefined, undefined

???

That’s because there are no call arguments.


#3

Im sorry Roy, but I really don’t understand your response.
Thanks anyway though.


#4
 > const determineWinner = (userChoice, computerChoice) => {
     console.log(userChoice, computerChoice)
     if (userChoice === computerChoice) {
       return 'we have a tie!';
     }
     // ...
   }
<- undefined
 > determineWinner()
   undefined undefined
<- "we have a tie!"
 >

#5

Hmm, maybe I’m missing a lot here.

wait, i understand now, because both the values are undefined…

In the final log statement I have tried passing the call arguements (before i started this post), however that also returns an error message.


#6

Notice the response in the console?

undefined undefined

That is because there are no arguments in the call. Parameter variables are local, not global so the globals are shadowed.

Show us your code with the arguments included, and post the error message.


#7

Okay all done now!
I probably used too many hints to actually get anything out of that project,
But thanks for helping me to see why those variables were returning as undefined!


#8

so, after finishing the challenge, I wanted to build on the cheat section, and add in another rule for what will happen if the computer threw a bomb, and also what would happen if we both throw bombs.

I followed the rules as closely as possible and as you can see from the code, altered the Math.random function to multiply by four to account for the extra option,

but now the output is that the computer will always throw a bomb, despite testing it several times.

to trouble shoot, I have tried moving the blocks of IF statements to different lines so they are interpreted either earlier or later, but its not working.

could anyone offer any advice here?

TIA :slight_smile:

const getUserChoice = (userInput) => {
  
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('error! Play rock, paper or scissors');
  };
 };

const getComputerChoice = () => {
  
	Math.floor(Math.random() * 4);
  
  if (getComputerChoice === 0) {
    return 'rock';
  } else if (getComputerChoice === 1) {
      return 'scissors';
    } else if (getComputerChoice === 2) {
       return 'paper';
    } else { 
      return 'bomb';
    
    } /* if/else statement close */ 
  }; /*function close */

const determineWinner = (userChoice, computerChoice) => {
  
  if (userChoice === 'bomb' && computerChoice === 'bomb') {
    return 'you both threw bombs.... everyone is crispy dead'
  
  } /* if close */
  
   if (userChoice === 'bomb') {
    return 'you threw the bomb and blew up the world!'
  
  } /* if close */
  
  if (computerChoice === 'bomb') {
    return 'the machines have bombs.... we all dead now';
  }
  
  if (userChoice === computerChoice) {
  
    return 'tie';
  } /*if statement close */
  
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
    
      return 'computer wins'; 
    
    } else {
      return 'player wins';
    
    } /* if/else close */ 
  } /* if close */

  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'computer wins' 
    
    } else {
      return 'player wins'
   
    } /* if/else close */
  } /* if close */ 
  
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'computer wins' 
    
    } else {
      return 'player wins;'
    
    } /* if/else close */
  
  } /* if close */
  

} /*function close */

const playGame = () => {
  
  const userChoice = getUserChoice('rock'); 
  const computerChoice = getComputerChoice();
  
  console.log(`you threw ${userChoice}`);
  console.log(`computer threw ${computerChoice}`);
  
  console.log(determineWinner(userChoice, computerChoice));
  
} /* function close */

playGame();


#9

That line is not doing anything. It needs to be assigned.

getComputerChoice is a function, not a variable. Use a different name and assign the random number to it, then test.

The idea behind the bomb is to give the user the upper hand. The computer should not have a bomb option.


#10

Thanks for the input once again!

previous to adding in these extra rules, the program worked as it should?


#11

your tip on assigning the Math.floor method a variable name has worked.

I nested the code inside of a switch statement (instead of the if/else) with the randomNumber as the parameter instead of the getComputerChoice();

thanks for your help!!! :slight_smile:

const getUserChoice = (userInput) => {
  
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('error! Play rock, paper or scissors');
  };
 };

const getComputerChoice = () => {
  
	const randomNumber = Math.floor(Math.random() * 4);
  
  switch(randomNumber) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'scissors';
      break;
    case 2: 
      return 'paper';
    default: 
      return 'bomb';
  
  } /*
  
  if (getComputerChoice === 0) {
    return 'rock';
  } else if (getComputerChoice === 1) {
      return 'scissors';
    } else if (getComputerChoice === 2) {
       return 'paper';
    } else { 
      return 'bomb';
    
    } /* if/else statement close */
  
  
  }; /*function close */

const determineWinner = (userChoice, computerChoice) => {
  
  if (userChoice === 'bomb' && computerChoice === 'bomb') {
    return 'you both threw bombs.... everyone is crispy dead'
  
  } /* if close */
  
   if (userChoice === 'bomb') {
    return 'you threw the bomb and blew up the world!'
  
  } /* if close */
  
 if (computerChoice === 'bomb') {
    return 'the machines have bombs.... we all dead now';
  } 
  
  if (userChoice === computerChoice) {
  
    return 'tie';
  } /*if statement close */
  
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
    
      return 'computer wins'; 
    
    } else {
      return 'player wins';
    
    } /* if/else close */ 
  } /* if close */

  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'computer wins' 
    
    } else {
      return 'player wins'
   
    } /* if/else close */
  } /* if close */ 
  
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'computer wins' 
    
    } else {
      return 'player wins;'
    
    } /* if/else close */
  
  } /* if close */
  

} /*function close */

const playGame = () => {
  
  const userChoice = getUserChoice('rock'); 
  const computerChoice = getComputerChoice();
  
  console.log(`you threw ${userChoice}`);
  console.log(`computer threw ${computerChoice}`);
  
  console.log(determineWinner(userChoice, computerChoice));
  
} /* function close */

playGame();


#12

break after return is unreachable. Don’t use break in a case that has a return.

For the sake of advancement, consider using an array instead of logic.

return ['rock', 'paper', 'scissors', 'bomb'][Math.floor(Math.random() * 4)];

#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.