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

274
Visualizações
can not use function to find sum of a part of an array (javascript)

I had 2 test cases like these (and maybe another one), some arrays like those one :

input: [{
  name: 'Javascript',
  coin: 1000
}, {
  name: 'PHP',
  coin: 1200
}, {
  name: 'Dart',
  coin: 1400
}, {
  name: 'Ruby',
  coin: 1600
}, {
  name: 'ReactJS',
  coin: 1600
}, {
  name: 'React-Native',
  coin: 1800
}, ]

or

intput: [{
  name: 'Javascript',
  coin: 1000
}, {
  name: 'PHP',
  coin: 1200
}, {
  name: 'Dart',
  coin: 1400
}]

I must write a javascript program to find a sum of the coin, for example, the 1st one is 8600 , the 2nd one is 3600.

I must use "reduce" function.

Here is mine

function run(courses) {
  var i = 0;
  var totalcoin = courses.reduce (function(total, course) {
    i++;
    return total + course.coin;
  },0);

But it said to me that I had the error : Error: expected undefined to equal 8600.

Could you please give me some ideas for this problem ? Thank you very much for your time.

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

0

Did you mean something like this?

your run function is incomplete, it should be returning the totalCoins value after reducing the array, and you need to run the function inorder to actually get the value

const array_first = [{ name: 'Javascript', coin: 1000 }, { name: 'PHP', coin: 1200 }, { name: 'Dart', coin: 1400 }, { name: 'Ruby', coin: 1600 }, { name: 'ReactJS', coin: 1600 }, { name: 'React-Native', coin: 1800 }]
const array_second = [{ name: 'Javascript', coin: 1000 }, { name: 'PHP', coin: 1200 }, { name: 'Dart', coin: 1400 } ];

const sum_first = run(array_first);
const sum_second = run(array_second);

console.log(`sum of coins in first array => ${sum_first}`);
console.log(`sum of coins in second array => ${sum_second}`);


function run(array) {
    const sum = array.reduce((acc, curr) => acc + curr.coin, 0);
    return sum;
}

Reference Material: Array.prototype.reduce()

about 4 years ago · Juan Pablo Isaza Relatório

0

The "reduce" function, like the other array functions, is itself a looping process -> so no need to have an iterator variable. So, something like

const sumOfCoins = courses.reduce((accumulator, course) => accumulator + course.coin, 0);

should give you the right answer. The reduce function goes through all elements of the array (course). The accumulator represents the current calculated value. So in your case, the accumulator gets "dragged along", each element is then added to the previous sum. The 0 at the end is the start value, so the accumulator will be 0 for the first operation. The return value of courses.reduce then is the last computed value.

about 4 years ago · Juan Pablo Isaza Relatório

0

A simple forEach loop can help you in this case:

So basically you have to loop through each element in the array and add/concatenate the value of coin in that element to a variable.

var input = [ { name: 'Javascript', coin: 1000 }, { name: 'PHP', coin: 1200 }, { name: 'Dart', coin: 1400 }, { name: 'Ruby', coin: 1600 }, { name: 'ReactJS', coin: 1600 }, { name: 'React-Native', coin: 1800 }, ]

function sumOfCoins(coinsArray){
  let totalCoins = 0;
input.forEach((element)=>{
  totalCoins += element.coin;
})
console.log(totalCoins); // 8600
}
sumOfCoins(input);
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