Rock Paper Scissors Javascript - What's wrong with my code?


#1

Hi I'm doing working on this JavaScript tutorial linked here. https://www.codecademy.com/courses/learn-javascript/projects/rock-paper-scissors
I think it's different from other versions of Rock Paper Scissors as the directions look different from other Rock Paper Scissors projects on here.
I could use some help. Right now it outputs in the console waaaay too many blocks of code such as the getUserChoice code block.
Anyways any help would be much appreciated!

function getUserChoice(){
  var userInput = prompt('Rock, paper or scissors?');
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('That\'s no good bro');  
  }
}

function getComputerChoice(){
  Math.floor(Math.random() * 3);
 switch(getComputerChoice) {
   case 0:
     return 'rock';
   case 1:
     return 'paper';
   case 2:
     return 'scissors';
             } 
}

function determineWinner(userChoice, computerChoice){
 if (userChoice === computerChoice) {
     return 'It\'s a tie!';
     }
  if (userChoice === 'rock'){
    if (computerChoice === 'paper') {
      return 'The Computer won!';
    } else { 
      return 'The Human won!';
    }
  }
  if (userChoice === 'paper') {
    if(computerChoice === 'scissors') {
      return 'The Computer won!';
    } else {
      return 'The Human won!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The Computer won!';
    } else {
      return 'The Human won!';
    }
  } 
}

function playGame(){
  var userChoice = getUserChoice();
  var computerChoice = getComputerChoice();
  console.log('You threw:' + getUserChoice);
  console.log('The Computer threw:' + getComputerChoice);
  console.log( determineWinner(userChoice, computerChoice));
}
playGame();

#2

Did you mean to write,

computerChoice = Math.floor(Math.random() * 3)
switch (computerChoice) {

?


#3

Yes you're right about that.

However the console log is spitting this out:

You threw:function getUserChoice(){
  var userInput = prompt('Rock, paper or scissors?');
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('That\'s no good bro');
  }
}
The Computer threw:function getComputerChoice(){
computerChoice =  Math.floor(Math.random() * 3);
 switch(computerChoice) {
   case 0:
     return 'rock';
   case 1:
     return 'paper';
   case 2:
     return 'scissors';
             }
}
It's a tie!

#4

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