Rock Paper Scissors
Programming Basics · JavaScript
TASK
Problem
Given two moves: `rock`, `paper`, or `scissors`. Print `FIRST`, `SECOND`, or `DRAW`.
EXAMPLE
Example
rock scissors
FIRST
LIMITS
Constraints
both moves are valid
LEARN
Theory for this problem
+
### Rock Paper Scissors
JavaScript arithmetic works with numeric values stored in variables. A clear solution reads the needed values, computes the formula with `+`, `-`, `*`, `/` or `**`, and prints only the final result.
NEED HELP?
Hints
+
['Write the formula using named intermediate values if it contains more than one operation.', 'Separate input parsing, result computation, and output into distinct steps so errors are easier to spot.']
ANSWER
Solution
+
const fs = require('fs');
const [first, second] = fs.readFileSync(0, 'utf8').trim().split(/\s+/);
if (first === second) {
console.log('DRAW');
} else {
const firstWins =
(first === 'rock' && second === 'scissors') ||
(first === 'scissors' && second === 'paper') ||
(first === 'paper' && second === 'rock');
console.log(firstWins ? 'FIRST' : 'SECOND');
}