Rock Paper Scissors - keeps showing a "tie"

Hi there! I’m working on the Rock Paper Scissors exercise, and everything seems to work correctly except that it keeps logging “the game is a tie:”. All of my curlies are in the right place, so I’m not sure where the issue. Any ideas? Thank you!

I’ve copy/pasted the actual code into the body of this post, but since it stripped the formatting I also included a screenshot from the page.


const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === “rock” || userInput === “paper” || userInput === “scissors” || userInput === “bomb”) {
return userInput
} else {
console.log(“Error!”);
}
};

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

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “The game is a tie!”;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return “Sorry, computer won!”;
} else {
return “Congratulations, you won!”;
}
}
if (userChoice === “bomb”) {
return “You won!!!”
}
};

const playGame = () => {
const userChoice = getUserChoice(‘bomb’);
const computerChoice = getComputerChoice();
console.log(You threw ${userChoice});
console.log(Computer threw ${computerChoice});
determineWinner(userChoice, computerChoice);
console.log(determineWinner());
}

playGame();

Hi, could you paste your code formatted? It would make it easier to read and find the error.
After you copy it,
You can use
Ctrl + Shift + C
and paste your code inside the thing that pops up. So your code can show up like this:

console.log('Hello')
2 Likes

Hello @css6918953431, welcome to the forums! The problem is with this line:

Here, you call determineWinner within a console.log(), but you provide no parameters for the function, where it accepts two. In JavaScript, unlike most languages, the complier automatically assigns any missed parameters to undefined. So in this case, your determineWinner function is comparing undefined to undefined-and of course undefined === undefined is true, so this code executes:

I hope this helps!
P.S. To see how to format your code like @h1lo did, visit this thread:

3 Likes

Hi @codeneutrino, thank you for the tip! Adding the arguments to the functions worked perfectly. :). I reviewed the article you and @h1lo shared so I’ll make sure to format my code correctly next time. I appreciate the help!

2 Likes