Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

594
Vistas
How to use .includes() with multiple conditions?

I'm building Yahtzee with React Redux. In my scores reducer I'm calculating whether or not to award the 30 points for a small straight based on the dice state. The dice state is expressed as an array and so i have to detect whether or not this array includes 1, 2, 3, 4 ... 2, 3, 4, 5 ... or 3, 4, 5, 6. What I came up with works but it looks very messy (below). Is there a cleaner way to check if these sets of values appear within an array?

    setSmallStraight: (state, action) => {
        if (
            action.payload.includes(1)
            && action.payload.includes(2)
            && action.payload.includes(3)
            && action.payload.includes(4)
            ||
            action.payload.includes(2)
            && action.payload.includes(3)
            && action.payload.includes(4)
            && action.payload.includes(5)
            ||
            action.payload.includes(3)
            && action.payload.includes(4)
            && action.payload.includes(5)
            && action.payload.includes(6)
        ) {
            state['Small Straight'] = 30
        } else {state['Small Straight'] = 0} 
    },
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use array.some() and array.every()

const small_straights = [
    [1, 2, 3, 4],
    [2, 3, 4, 5],
    [3, 4, 5, 6]
];

if (small_straights.some(dice => dice.every(die => action.payload.includes(die))) {
    state['Small Straight'] = 30;
} else {
    state['Small Straight'] = 0;
}
over 4 years ago · Santiago Trujillo Denunciar

0

Using Array#some, Array#every, and Set:

const _hasAll = (arr = [], nums = []) => {
  const set = new Set(arr);
  return nums.every(num => set.has(num));
}
const shouldAward = ({ payload = [] }) =>
  [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]].some(nums => _hasAll(payload, nums));
  
console.log( shouldAward({ payload: [1, 2, 3, 4] }) );
console.log( shouldAward({ payload: [2, 3, 4, 5] }) );
console.log( shouldAward({ payload: [3, 4, 5, 6] }) );
console.log( shouldAward({ payload: [4, 5, 6, 7] }) );

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda