Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda