Project Euler - Problem 1 | JavaScript one-liner

in #programming6 years ago (edited)

Project Euler 001 One Line.jpg

A few days ago, in my post Learning Clojure - Part 2 | Writing a Basic Function, I solved the first Project Euler problem in JavaScript, as preparation to solve that very same problem in Clojure.

My JavaScript solution is included below:

let sum = 0
for (let i = 0; i < 1000; i++) {
  if (i % 3 == 0 || i % 5 == 0) {
    sum += i
  }
}
console.log(sum) // => 233168

In that same post, I also wrote that had I spent a little more time, I could probably come up with a neat one-liner as well.

And so I did:
console.log(Array.from(Array(1000).keys()).filter(x => x%3 === 0 || x%5 === 0).reduce((a, b) => a + b));

// => 233168