Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!