-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path001.js
32 lines (21 loc) · 763 Bytes
/
001.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
Source: https://projecteuler.net/problem=1
Problem:
"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.
Find the sum of all the multiples of 3 or 5 below 1000."
My Algorithm in words:
1. Define a variable to hold the sum
2. Run a for-loop from 1 to 1000
3. If the number is a multiple of 3 or 5, add the number to the sum variable
4. Return the sum variable
*/
function solution() {
var sum = 0;
for (let i = 0; i < 1000; i++) {
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
sum += i;
}
}
return sum;
}
console.log(solution()) //outputs the answer!