Home > Software design >  How to use .includes() with multiple conditions?
How to use .includes() with multiple conditions?

Time:02-22

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} 
    },

CodePudding user response:

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;
}

CodePudding user response:

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] }) );

  • Related