Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda