Build Rock, Paper or Scissors help please


#1

This is the project exercise BTW.

function getUserChoice () {
  var userInput = prompt("Rock, paper or scissors?").toLowerCase();
  
  if (userInput === "rock" || userInput === "paper" || userInput === "scissors") {
    return userInput;
  } else {
    console.log("Error!");
  }
}

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

function determineWinner (userChoice, computerChoice) {
  
  if (userChoice === computerChoice) {
    return "The result is a tie!";
  } else {
    return "Not a tie!";
  }
}

console.log(determineWinner(getUserChoice, getComputerChoice));

The first two functions work well.
The 3rd one no, and I can't figure it out (been stuck on it for hours). It keeps logging "Not a tie!" to the console, even when it shouldn't.

I've done many tests, and it keeps logging the "else" part.

Thanks.


#2
function getComputerChoice() {
    return ['rock','paper','scissors'][Math.floor(Math.random() * 3)];
}

var computerChoice = getComputerChoice();

#3

Hi Alexandre :slight_smile:

All the code you've written for the project so far is perfectly fine (you've completed 5 tasks out of 11).

But the final console.log statement you've written yourself to test how things are going needs to be re-written as:

console.log(determineWinner(getUserChoice(), getComputerChoice()));

Once you make those changes, the statement will correctly call the getUserChoice and getComputerChoice functions, and you'll see that the determineWinner function is working perfectly :slight_smile:

Then, you can remove the else part of determineWinner and the final console.log statement, and move on to Task 6.

BTW I like what you did with the toLowerCase function for Task 2 – that's neater than the code provided in the Hint :slight_smile:


#4

The course has not covered arrays yet :slight_smile: That comes in the next lesson, "Arrays and Loops"!


#5

Thank you very much. Make a lot of sense to add both ()s as I'm calling functions as parameters.


#6

But what if the user clicks Cancel or presses the Esc
key?

Thanks. It's always been my contention that RPS was introduced too soon, and should have at least followed data structures and loops. It's a project that introduces concepts (or the need for some) not yet learned, and projects shouldn't do that when they are for beginners. They give up too soon if they can't figure them out.