Rock, Paper, Scissors


#1

I have been working on this all day. Everything seems to work properly as far as round tracking and accepting upper- and lowercase input. It even subtracts from the round count if you enter an invalid response.

But it will not display the game results at the end of the code.

What am I missing here???

// Computer's selection
function computerPlay() {
  var selectionArr = ['rock', 'paper', 'scissors'];
  return selectionArr[Math.floor(Math.random() * selectionArr.length)];
};

// Points initialize
var w = 0;
var l = 0;

// Play a single round
function playRound(playerSelection, computerSelection) {      
  if (playerSelection.toLowerCase() === 'rock') {
    if (computerSelection === 'scissors') {
      return 'You Win! Rock beats scissors.';
      w++;
    } else if (computerSelection === 'paper') {
      return 'You Lose! Paper beats rock.';
      l++;
    } else {
      return 'Tie!';
    }
  } else if (playerSelection.toLowerCase() === 'paper') {
    if (computerSelection === 'rock') {
      return 'You Win! Paper beats rock.';
      w++;
    } else if (computerSelection === 'scissors') {
      return 'You Lose! Scissors beats paper.';
      l++;
    } else {
      return 'Tie!';
    }
  } else if (playerSelection.toLowerCase() === 'scissors') {
    if (computerSelection === 'rock') {
      return 'You Lose! Rock beats scissors.';
      l++;
    } else if (computerSelection === 'paper') {
      return 'You Win! Scissors beats paper.';
      w++;
    } else {
      return 'Tie!';
    } 
  } else {
      alert('You did not enter a valid choice. Try again.');
      round--;
  }
};

// 5 rounds against the AI
function game() {
  round = 0;
  while (round < 5) {
    playerSelection = prompt('Enter rock, paper, or scissors.');
    computerSelection = computerPlay();
    console.log(playRound(playerSelection,computerSelection));
    round++;
  }
  
  if (w > l) {
    return 'You Win the Game!';
  } else if (w < l) {
    return 'You Lose the Game!';
  } else {
    return 'It\'s a draw!';
  }
};

game();

#2

You are just returning a string in your final if statement. To get it to show to the console you could wrap the game(); function call in console.log.

console.log(game())

This will give you the desired response.


Another solution would be to log inside the game() function itself. Instead of returning a string, console.log it inside your if statement.

  if (w > l) {
    console.log('You Win the Game!');
  } else if (w < l) {
    console.log('You Lose the Game!');
  } else {
    console.log('It\'s a draw!');
  }

And then call game(); like you have been.


#3

Ok, that definitely made some progress. It now returns the results in the console!

The problem now is that it always returns ‘It’s a draw!’ no matter how many rounds were won or lost…


#4

You are returning before you increment w or l


#5

But aren’t they set to increment within the playRound() function? I’m sure I’m missing the obvious here. Forgive me. I just started learning this stuff.


#6
 if (computerSelection === 'scissors') {
      return 'You Win! Rock beats scissors.';
      w++;

Notice how you have the return before the w++;? return ends the function is is called in immediately, no questions asked. w++ is never run, no matter what.

Try this:

function earlyReturn() {
  return "I'm an early return, anything below me doesn't get run";
  console.log('I never get run, ever, no matter what');
}

and read this: http://www.itamarweiss.com/personal/2018/02/28/return-early-pattern.html


#7

That definitely fixed the problem. Thanks! Can’t believe I missed something that simple.

After reading the article, is this how I should have formatted the code?

function computerPlay() {
  var selectionArr = ['rock', 'paper', 'scissors'];
  return selectionArr[Math.floor(Math.random() * selectionArr.length)];
};

// Points initialize
var w = 0;
var l = 0;

// Play a single round
function playRound(playerSelection, computerSelection) {      
  if (playerSelection.toLowerCase() === 'rock') {
    if (computerSelection === 'scissors') {
      w++;
      return 'You Win! Rock beats scissors.';
    }
    
    if (computerSelection === 'paper') {
      l++;
      return 'You Lose! Paper beats rock.';
    } 
    
    else {
      return 'Tie!';
    }
    
  } else if (playerSelection.toLowerCase() === 'paper') {
    if (computerSelection === 'rock') {
      w++;
      return 'You Win! Paper beats rock.';
    } 
    
    if (computerSelection === 'scissors') {
      l++;
      return 'You Lose! Scissors beats paper.';
    } 
    
    else {
      return 'Tie!';
    }
    
  } else if (playerSelection.toLowerCase() === 'scissors') {
    if (computerSelection === 'rock') {
      l++;
      return 'You Lose! Rock beats scissors.';
    } 
    
    if (computerSelection === 'paper') {
      w++;
      return 'You Win! Scissors beats paper.';
    } 
    
    else {
      return 'Tie!';
    } 
    
  } else {
      alert('You did not enter a valid choice. Try again.');
      round--;
  }
};

// 5 rounds against the AI
function game() {
  round = 0;
  while (round < 5) {
    playerSelection = prompt('Enter rock, paper, or scissors.');
    computerSelection = computerPlay();
    console.log(playRound(playerSelection,computerSelection));
    round++;
  }
  
  if (w > l) {
    return 'You Win the Game! Player: ' + w + ', Computer: ' + l ;
  } 
  
  if (w < l) {
    return 'You Lose the Game! Player: ' + w + ', Computer: ' + l ;
  } 
  
  else {
    return 'It\'s a draw!';
  }
};

game();

#8

Don’t worry too much about what the article says as a pattern. Your code looks fine to me!

I was just trying to drive home what an early return does. :wink: