___  ___    _ _    _  _ _____   _____
 / __|/ _ \  | | |  | || |_ _\ \ / / __|
| (_ | (_) | |_  _| | __ || | \ V /| _|
 \___|\___/    |_|  |_||_|___| \_/ |___|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Project Euler - Problem 1 | JavaScript one-liner

BY: @oyvindsabo | CREATED: July 7, 2018, 11:34 a.m. | VOTES: 5 | PAYOUT: $0.44 | [ VOTE ]

[IMAGE: https://ipfs.busy.org/ipfs/QmYayhwWaik3FvGR9fQ7D42GGWWBk7uZ9rMDwDkLPG7bnP]

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

TAGS: [ #programming ] [ #dev ] [ #project-euler ] [ #javascript ] [ #blog ]

Replies

NO REPLIES FOUND.

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>