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

119
Views
Alternative of "if statement" with "switch statement

I'm currently write the litte Scissors, paper, rock game and I used "If statement" to do so. Now, I try to write it with the "switch statement". So I will show you what I wrote but nothing happen in my console and I don't know why. Seems pretty easy for anybody used to code everyday I think.

So I begin to declare my variables.

let rock = 'rock';
let scissors = 'scissors';
let paper = 'paper';

then these two variables will be changed to test the conditional

let firstPlayerChoice = paper;
let secondPlayerChoice = rock;

After that I made my seven possibilities

let firstPossibility = firstPlayerChoice === paper && secondPlayerChoice === rock;
let secondPossibility = firstPlayerChoice === scissors && secondPlayerChoice === paper;
let thirdPossibility = firstPlayerChoice === rock && secondPlayerChoice === scissors;

let fourthPossibility = firstPlayerChoice === rock && secondPlayerChoice === paper;
let fifthPossibility = firstPlayerChoice === paper && secondPlayerChoice === scissors;
let sixthPossibility = firstPlayerChoice === scissors && secondPlayerChoice === rock;

let seventhPossibility = firstPlayerChoice === secondPlayerChoice;

So, now I create my condition using my Switch statement but nothing is running on my console. The fact is I have difficulties to identify the good expression.

switch (firstPlayerChoice) {
    case firstPlayerChoice === paper && secondPlayerChoice === rock:
    case firstPlayerChoice === scissors && secondPlayerChoice === paper:
    case firstPlayerChoice === rock && secondPlayerChoice === scissors:
        console.log('First Player won !');
        break;
}

switch (secondPlayerChoice) {
    case fourthPossibility:
    case fifthPossibility:
    case sixthPossibility:
        console.log('Second player won !');
        break;
}

Someone can provide me just one expression to help to understand the big picture please ?

about 4 years ago · Juan Pablo Isaza
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!