3

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

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.