[Solved] JavaScript loop through two arrays


Do you want something like this?

const biscuitsPerCat = (cats, biscuits) => {
  const share = Math.floor(biscuits / cats.length)
  const cutoff = biscuits - cats.length * share
  return cats.reduce((res, cat, idx) => ({...res, [cat]: idx < cutoff ? share + 1 : share}), {})
}

console.log(biscuitsPerCat(["Andy", "Bandy", "Candy", "Dandy"], 14))

… which you could then use to format your output?

solved JavaScript loop through two arrays