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

109
Views
Trouble simplifying conditional

I'm having trouble simplifying a conditional statement. It looks like this:
There is an array of arrays like this, dice = [ [a, b], [c, d] ].
a, b, c, and d representing random numbers on a dice 1 > 6.
Every array in the array represents a roll of two dice where the last roll is [c, d] = [2, 5] for example,
and the second-last roll is [a, b] = [3, 6] for example.
Now if you roll 2 x 1 in a row or 2 x 6 in a row something happens. so, a + c || a + d and b + c || b + d may not be 2 or 12.
The conditional below works but I think if you open the door to hell it looks prettier.
dice = last roll = [c, d]
prev = second last roll = [a, b]

if (dice[0] + prev[0] === 2 || dice[1] + prev[1] === 2 || dice[1] + prev[0] === 2 || dice[0] + 
prev[1] === 2 &&
dice[0] + prev[0] === 12 || dice[1] + prev[1] === 12 || dice[1] + prev[0] === 12 || dice[0] + 
prev[1] === 12){
//something happens here
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I would do something like this:

var combinations = dice.reduce(function(acc, curr) {
    return acc.concat(prev.map(function(curr2) {
        return curr + curr2;
    }));
}, []);

if (combinations.indexOf(2) !== -1 && combinations.indexOf(12) !== -1) {
    //something happens here
}

In ES6 it is a little bit shorter:

const combinations = dice.reduce((acc, curr) => acc.concat(prev.map(curr2 => curr + curr2)), []);

if (combinations.indexOf(2) !== -1 && combinations.indexOf(12) !== -1) {
    //something happens here
}

What this actually does is calculate all combinations (additions) and then check whether one is 2 and one is 12.

about 4 years ago · Juan Pablo Isaza Report

0

I came out with a simpler solution more fitted to your need

const throws1 = [[1, 2], [3, 4]]
const throws2 = [[1, 2], [3, 1]]
const throws3 = [[1, 6], [6, 1]]


const checkCondition = (prev, dice) => [1, 6].every(res => prev.includes(res) && dice.includes(res))

console.log(checkCondition(...throws1))
console.log(checkCondition(...throws2))
console.log(checkCondition(...throws3))

about 4 years ago · Juan Pablo Isaza Report

0

The condition can only happens when both dice and prev contain 1 or 6. So, we can just check that.

let throws = [[5,3],[1,6]];
let prev = throws[0];
let dice = throws[1];

if ((prev.includes(1) && dice.includes(1)) || (prev.includes(6) && dice.includes(6))){
  console.log("2 or 12 occurs")
}
else {
  console.log("2 and 12 do not occur")
}

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