Skip to content

Commit 1b00391

Browse files
authored
Create 001.js
1 parent 5c4114f commit 1b00391

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

001.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Source: https://projecteuler.net/problem=1
3+
4+
Problem:
5+
6+
"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
7+
8+
Find the sum of all the multiples of 3 or 5 below 1000."
9+
10+
11+
My Algorithm in words:
12+
13+
1. Define a variable to hold the sum
14+
2. Run a for-loop from 1 to 1000
15+
3. If the number is a multiple of 3 or 5, add the number to the sum variable
16+
4. Return the sum variable
17+
18+
*/
19+
20+
function solution() {
21+
var sum = 0;
22+
23+
for (let i = 0; i < 1000; i++) {
24+
if ((i % 3 === 0) || (i % 5 === 0)) { //to check whether i is divisble by 3 or 5, we check the remainder (with mod) when i is divided by 3 or 5
25+
sum += i;
26+
}
27+
}
28+
29+
return sum;
30+
}
31+
32+
console.log(solution()) //outputs the answer!

0 commit comments

Comments
 (0)