I'm following a JS course for beginners and the first lesson was about making a "rock, paper, scissors" game.
The instructor said something about this not being the most efficient way to determine the winner but didn't really touch on how to write it. All I can think of is using Switch statement instead of if.
Can someone show me how it should be done?
function getResult(){
if (computerChoice === userChoice) {
result = "It's a draw";
} else if (computerChoice == "rock" && userChoice == "scissors") {
result = 'You lost';
} else if (computerChoice == "rock" && userChoice == "paper") {
result = 'You won!';
} else if (computerChoice == "paper" && userChoice == "scissors") {
result = 'You won';
} else if (computerChoice == "paper" && userChoice == "rock") {
result = 'You lost';
} else if (computerChoice == "scissors" && userChoice == "rock") {
result = 'You won';
} else if (computerChoice == "scissors" && userChoice == "paper") {
result = 'You lost';
}
resultDisplay.innerHTML=result;
}
Thanks, Amin
If you associate a number with each possible choice, you can then subtract them from each other. If it isn't a tie, there are only 2 possible options left (after modulo), which allows you to concisely determine who won.
const choices = ['rock', 'paper', 'scissors'];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
console.log(getResult(choices[i], choices[j]));
}
}
function getResult(computerChoice, userChoice){
const desc = `Comp: ${computerChoice}, you: ${userChoice}: `;
if (computerChoice === userChoice) {
return desc + "It's a draw";
}
const diff = (3 + choices.indexOf(computerChoice) - choices.indexOf(userChoice)) % 3;
return desc + (diff === 2 ? 'You won' : 'You lost');
}
You can create an object that defines if your move is weak or strong against another.
See bellow code:
function getResult() {
const weapons = {
rock: {
weakTo: 'paper',
strongTo: 'scissors'
},
paper: {
weakTo: 'scissors',
strongTo: 'rock'
},
scissors: {
weakTo: 'rock',
strongTo: 'paper'
}
}
if (weapons[userChoice].strongTo === computerChoice) {
result = "You Won";
} else if (weapons[userChoice].weakTo === computerChoice) {
result = "You lost";
} else {
result = "It's a draw";
}
resultDisplay.innerHTML = result;
}
When solving a problem like this, it's good to think about your data model first. How would you create a data structure that made it easy to answer the question you need to answer?
Maybe something like:
const winsVs = {
rock: 'scissors',
paper: 'rock',
scissors: 'paper'
}
Now winsVs.rock === 'scissors' which means that rock wins versus scissors.
Now you have an if condition with just the three cases: tie, win and lose.
const winsVs = {
rock: 'scissors',
paper: 'rock',
scissors: 'paper'
}
function getResult(computerChoice, userChoice) {
let result
if (computerChoice === userChoice) {
result = "It's a draw"
} else if (winsVs[computerChoice] === userChoice) {
result = "You lost"
} else if (winsVs[userChoice] === computerChoice) {
// could also be just `else {` since only one option is left.
result = "You won"
}
return result
}
console.log(getResult('paper', 'paper')) // It's a draw
console.log(getResult('rock', 'paper')) // You won
console.log(getResult('paper', 'rock')) // You lost