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

595
Views
¿Cómo usar .includes() con múltiples condiciones?

Estoy construyendo Yahtzee con React Redux. En mi reductor de puntajes, estoy calculando si otorgar o no los 30 puntos por una escalera pequeña según el estado de los dados. El estado de los dados se expresa como una matriz, por lo que tengo que detectar si esta matriz incluye o no 1, 2, 3, 4... 2, 3, 4, 5... o 3, 4, 5, 6. ¿Qué Se me ocurrió que funciona, pero se ve muy desordenado (abajo). ¿Existe una forma más limpia de verificar si estos conjuntos de valores aparecen dentro de una matriz?

 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 answers
Answer question

0

Puede usar array.some() y 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 Report

0

Usando Array#some , Array#every y 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 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!