Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
A cleaner way to write this block of JS code

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

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

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');
}

about 4 years ago · Santiago Gelvez Report

0

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;
}

about 4 years ago · Santiago Gelvez Report

0

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

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!