Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
How to use reduce method within map method to add numbers

Not sure if I am overcomplicating things but I am trying to get the sum of all the numbers inside this array of arrays:

const frames = [
  [2, 0], [4, 2], [6, 0], [2, 4], [1, 5], [7, 0], [5, 2], [7, 0], [2, 6], [8, 1]
]

I am practising using map and reduce to do so:

const score = (frames) =>{
  console.log(frames)
  let addedScores = frames.map(frame.reduce((previousValue, currentValue) => previousValue + currentValue))
  console.log(addedScores)
}

But currently getting this error:

TypeError: 2,04,26,02,41,57,05,27,02,68,1 is not a function
    at Array.map (<anonymous>)
    at score (/Users/x/Desktop/Programming/devacademy/bootcamp/week1/preparation/bowling-kata/game.js:8:28)
    at Object.<anonymous> (/Users/x/Desktop/Programming/devacademy/bootcamp/week1/preparation/bowling-kata/game.js:17:1)

Here is a fiddle version.

Any advice and explanation would be greatly appreciated

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are almost there on this one! If you look at the stack trace for the error you are facing, you will see that an error is thrown by Array.map function, and namely that "stuff" (i.e 2,04,26,02,41,57,05,27,02,68,1) is "not a function".

The map higher-order function expects a function that it will map accross the elements of frames.

What you want is something like this:


//...
  let addedScores = frames.map((frame) => frame.reduce((previousValue, currentValue) => previousValue + currentValue))
//...


Here I have only converted your addedScores expression to pass an anonymous function: (frame) => { ... } to the map function.

Hope this helps!

The resulting shape for addedScores would be: [2, 6, 6, 6, 6, 7, ...], which is the sum of each pair of numbers in frames.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda