Computer throwing undefined


#1

I was almost finished with the rock paper scissors exercise but the computer keeps throwing undefined.
the problem is within the randomNumber spot after Math.random



const getUserChoice = userInput=>{
  userInput =
  userInput.toLowerCase();
  if (userInput === 'rock' ||
     userInput === 'paper' ||
     userInput === 'scissors'){
    return userInput;
  }//if block
  else{
    console.log('you dun goofed')
  }//else
}//end of first function
  const getComputerChoice = ()=>{
    Math.floor(Math.random()*2)
    switch(Math.random){
      case 0:
        return 'rock';
        break;
      case 1:
        return 'paper';
        break;
      case 2:
        return 'scissors';
        break;
     
       
        
                       }//switch
  }//end of comp choice function

const determineWinner = (userChoice, computerChoice)=>{
  if (userChoice === computerChoice){
    return 'Dun tied it up';
  }//if end
  
  if (userChoice === 'rock'){
    if (computerChoice === 'paper'){
      return 'The computer won!'
}//comp choice scissors
    else {
      return 'You won!';
    }//else end
  }//user choice rock end
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }//else end
  }//if end
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return 'The computer won!';
    }
    else {
      return 'You won!';
    }//else end
  }//if end
  
}//winner function
const playGame = () => {
    const userChoice = getUserChoice('scissors');
    const computerChoice = getComputerChoice();
    console.log('You threw: ' + userChoice);
    console.log('The computer threw: ' + computerChoice);
    console.log(determineWinner(userChoice, computerChoice));
}

playGame();





#2

i realize in what i pasted
const getComputerChoice = ()=>{
Math.floor(Math.random()*2)
switch(Math.random){
case 0:
return ‘rock’;
break;
i have math.random in the switch, at first it was randomNumber, but i put that in there to try and figure out the problem


#4

Hi @gnarlybrown,

Refer Math.random() documentation here
It returns the value of random number between 0 (inclusive) and 1 (exclusive).


For your case:

Math.random() * 2 only gives you the value from 0 to < 2.
Which means, Math.floor(Math.random()*2) only gives you the value of 0 and 1.

You need 3 cases, for value 0, value 1 and value 2.
So, your Math.random() needs to be multiplied by 3.


It is recommended that you assign the value of the Math.floor(Math.random()…) to the variable randomNumber. Then put it into the switch bracket, like this:

switch (randomNumber)

or else, it could be this way too (but messy)

switch (Math.floor(Math.random().........)

Cheers :slight_smile:


Some Notes:

  • For tidy code, you may consider making this into one line:

    userInput =
      userInput.toLowerCase();
    

    to userInput = userInput.toLowerCase();

  • For my preference, I’d use if/else if/else if instead of if/ if/ if, though both runs fine.

    if (userChoice === computerChoice){
      //code
    }
    else if (userChoice === 'rock'){
      //code
    }
    else if (userChoice === 'paper') {
     //code
    }
    else if (userChoice === 'scissors') {
     //code
    }
    
  • break is not needed after having a return

     case 0:
          return 'rock';
          break; // <== delete this, break is not reachable after return
     case 1:
          return 'paper';
          break;  // <== delete this
     case 2:
          return 'scissors';
          break; // <== delete this
    

#5

thank you very much for your input.
after going back through, i added a value to randomNumber and it solved the problem, it runs perfect now
i do have another question, someone on another forum mentioned i could have added the value to randomNumber in the switch instead of doing it the way i did. do you have any tips for this?

edit: also, you were right about the Math.random needing to be *3, cheers.,

aand going back looking at your previous suggestions, i see the alternate route i could have taken for adding the value to randomNumber. this way does look less messy, you’re right

const getComputerChoice= ()=>{
   let randomNumber=Math.floor(Math.random()*3)
    
   switch(randomNumber){
      case 0:
        return 'rock';
      case 1:
        return 'paper';
      case 2:
        return 'scissors';

#6

You’re welcome. :slight_smile:

I’m afraid I don’t quite understand your question.

const getComputerChoice= ()=>{
   let randomNumber=Math.floor(Math.random()*3)
    
   switch(randomNumber){
      case 0:
        return 'rock';
      case 1:
        return 'paper';
      case 2:
        return 'scissors';
   }
}

looks good for me. Much like how I’d write it. The other way without using variable would be:


#7

I worded it wrong, what the other person was referring to was the method without using the variable, not adding the value to the variable in the switch.
I did prefer to just use the variable, i think it looks neater.


#8

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