Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 680 Bytes

challenge-17.md

File metadata and controls

33 lines (24 loc) · 680 Bytes

Reto 17: Llevando los regalos en sacos

🔗 Enunciado

Solución

function carryGifts(gifts, maxWeight) {
  let currentWeight = 0

  return gifts
    .filter((gift) => gift.length <= maxWeight)
    .reduce((acc, current) => {
      const currentLength = current.length

      if (currentWeight + currentLength > maxWeight) {
        acc.push([current])
        currentWeight = currentLength
        return acc
      }

      if (currentWeight === 0) acc.push([])

      acc.at(-1).push(current)
      currentWeight += currentLength

      return acc
    }, [])
    .map((bag) => bag.join(' '))
}

🚀 140 puntos