How to Solve Euler 1 in JavaScript

The Euler 1 problem is fairly simple to solve. Most people intuitively know how to do it. Here is how I would do it in JavaScript.

Array.apply(null, Array(1000))
  .map(function (_, i) {
    return i;
  })
  .reduce(function (sum, cur) {
    if (cur % 3 == 0 || cur % 5 == 0) sum += cur;
    return sum;
  });

From left to right we do:

Related Posts