0

I am working through TheOdinProject's rock paper scissors game. My playRound function is not yet completed and I am planning on expanding the "not a tie" message with winning and losing messages after I resolve my current issue. Currently, the console logs "not a tie" for every outcome regardless of whether the user and the computer chose the same selection.

Additionally, I am wondering if I am doing the right thing by calling getUserChoice and getComputerChoice in the playRound function; if I make variables for userChoice and computerChoice outside the playRound function (ex. let userChoice = getUserChoice) it prompts the user for a selection twice which I do not want.

Initially I was missing a 'return' in my getUserChoice function and I thought adding it would fix my issue however the same issue still remains.

Any help is greatly appreciated!

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

function getUserChoice () {
let caseInsensitiveUserSelection = prompt("Rock, paper or scissors?");
let userChoice = caseInsensitiveUserSelection.toLowerCase();
return userChoice;
}

function playRound() {
    let userChoice = getUserChoice()
    let computerChoice = getComputerChoice()
    if (userChoice === computerChoice) {
    console.log("Tie game!") }
     else { console.log("not a tie") }
    
}
playRound()
kalper
  • 5
  • 1
  • 1
    `getComputerChoice` returns null every time, so it never will be equal to any user input. – Sergio Tulentsev Jul 18 '23 at 19:30
  • 2
    You're not returning any value in `getComputerChoice()` – technophyle Jul 18 '23 at 19:31
  • 3
    This is a good opportunity to start [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, what values do you observe for `userChoice` and `computerChoice` in the `playRound` function? Does each value match what you expect? If not, what did you expect that value to be *and why*? – David Jul 18 '23 at 19:33
  • `switch case` is lacking break for each case, you are returning nothing and also there is no need to write 3 returns. You could just save the result in 1 variable and return that once after your `switch case` – Chris G Jul 18 '23 at 19:49
  • @David thanks for the reminder, i learned about debugging before but applying it here reinforced a useful tool going forward – kalper Jul 19 '23 at 16:08

1 Answers1

-1

You're not returning any values from the getComputerChoice() function.

You'll need to modify your code like:

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

function getUserChoice() {
   let caseInsensitiveUserSelection = prompt("Rock, paper, or scissors?");
   let userChoice = caseInsensitiveUserSelection.toLowerCase();
   return userChoice;
}

function playRound() {
   let userChoice = getUserChoice();
   let computerChoice = getComputerChoice();

   if (userChoice === computerChoice) {
       console.log("Tie game!");
   } else {
       console.log("Not a tie");
   }
}

playRound();
Keygun2k1
  • 145
  • 4
  • 12