Tell us what’s happening:
Hello,
In this, when placing return within the if/else if/else statements, the first statement returns regardless if the condition is true/false. Example: “Player wins! Rock beats Rock!” in the console.log.
Else if and Else are skipped. Return ends the code there, but shouldn’t “if” skip its execution if false?
The function works as expected when placed where the comment is. The wrong answer is sometimes accepted too.
Your code so far
<!-- file: index.html -->
/* file: script.js */
// User Editable Region
function getRoundResults(userOption) {
const computerResult = getRandomComputerResult();
let x=``
if (hasPlayerWonTheRound){
playerScore++
x=`Player wins! ${userOption} beats ${computerResult}`
return x;
}
else if (computerResult===userOption){
x=`It's a tie! Both chose ${userOption}`
return x;
}
else{
computerScore++
x=`Computer wins! ${computerResult} beats ${userOption}`
return x;
}
//return x here works
}
console.log(getRoundResults("Rock"));
console.log("Player Score: ", playerScore, "Computer Score: ", computerScore);
// User Editable Region
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0
Challenge Information:
Review DOM Manipulation by Building a Rock, Paper, Scissors Game - Step 3