# Exercise 8 Sum of multiples of 3 and 5

**URL:** <https://forum.codewithmosh.com/t/exercise-8-sum-of-multiples-of-3-and-5/12167>\
**Category:** JavaScript\
**Created:** [April 28, 2022, 7:48pm UTC](https://forum.codewithmosh.com/t/exercise-8-sum-of-multiples-of-3-and-5/12167 "2022-04-28T19:48:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ItsPantherG](https://avatars.discourse-cdn.com/v4/letter/i/8edcca/32.png) [@ItsPantherG](https://forum.codewithmosh.com/u/ItsPantherG)\
**Post date:** [April 28, 2022, 7:48pm UTC](https://forum.codewithmosh.com/t/exercise-8-sum-of-multiples-of-3-and-5/12167/1 "2022-04-28T19:48:08Z")

</div>

Hey everyone,  
When I was following the JavaScript exercise 8 about sum of multiples of 3 and 5 I think I found a bud in the code Mosh showed in the video. I don’t know if this is not checked by him by mistake or if it was intentionally.

The exercise is to create a function that adds the multiples of 3 and 5 under a limit number:  
For example  
If the limit number is 10, the multiples of 3 and 5 are: 3, 6, 9 and 5, 10.  
Then he would add those and the result is 33.

But if the limit number is 15, the multiples are: 3, 6, 9, 12, 15 and 5, 10, 15.  
In this case 15 is double in the sequence and I realised in this code the result was 60 instead of 75 because he didn’t add the doubled 15.

The code he used:

console.log(sum(15));

function sum(limit) {  
let sum = 0;  
for (let i = 0; i \<= limit; i++) if (i % 3 === 0 || i % 5 === 0) sum += i;

return sum;  
}

I did try to code a function that would add numbers if they occur in both the multiples of 3 and 5 but its very long and since I’m really new to coding its probably not efficient, but it does work 😉

I was hoping if someone that knows more could also try to code this problem so I could see how you would solve this problem on a higher level than me XD

btw this is what I came up with:

sum(15);

function sum(limit) {  
i = 0;  
let sumMultipliedByThree = 0;  
let sumOfThree = 0;

while (sumMultipliedByThree \<= limit) {  
i++;  
sumOfThree += sumMultipliedByThree;  
sumMultipliedByThree = 3 \* i;  
}

i = 0;  
let sumMultipliedByFive = 0;  
let sumOfFive = 0;

while (sumMultipliedByFive \<= limit) {  
i++;  
sumOfFive += sumMultipliedByFive;  
sumMultipliedByFive = 5 \* i;  
}  
console.log(sumOfThree + sumOfFive);  
}

Thanks for the help in advance, this is really just a curiosity question for people who like to answer

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [April 28, 2022, 8:13pm UTC](https://forum.codewithmosh.com/t/exercise-8-sum-of-multiples-of-3-and-5/12167/2 "2022-04-28T20:13:46Z")

</div>

Hi,  
This must be on purpose because the way the for loop works `i` will have an eligible value once. So doubles do not apply

```auto
console.log(sum(15));

function sum(limit) {
	let sum = 0;
	for (let i = 0; i <= limit; i++) 
		if (i % 3 === 0 || i % 5 === 0)
			sum += i;

	return sum;
}

```

That said the exercice seems inspired by the FizzBuzz problem which is a bit different.

Your code is complex, verbose and uses multiple loops.  
I’d refactor like this

```auto
console.log(sum(15));

function sum(limit) {
	let sumBy3 = 0;
	let sumBy5 = 0;
	
	for (let i = 0; i <= limit; i++) {
		if (i % 3 === 0) sumBy3 += i;
		if (i % 5 === 0) sumBy5 += i;
	}

  return sumBy3 + sumBy5;
}

```

Regards

---

<div class="post-metadata">

**Author:** ![ItsPantherG](https://avatars.discourse-cdn.com/v4/letter/i/8edcca/32.png) [@ItsPantherG](https://forum.codewithmosh.com/u/ItsPantherG)\
**Post date:** [April 28, 2022, 10:24pm UTC](https://forum.codewithmosh.com/t/exercise-8-sum-of-multiples-of-3-and-5/12167/3 "2022-04-28T22:24:10Z")

</div>

thanks for the response, love to see how people write code that that seem so easy when its done and seem so hard when you have to write it XD

---

<div class="post-metadata">

**Author:** ![spiperman957](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@spiperman957](https://forum.codewithmosh.com/u/spiperman957)\
**Post date:** [July 4, 2022, 4:15pm UTC](https://forum.codewithmosh.com/t/exercise-8-sum-of-multiples-of-3-and-5/12167/4 "2022-07-04T16:15:41Z")

</div>

Hi - I was reading your answer to the op, and was wondering if you could clarify something for me.  
The variable statement **let sum;** does not work, and the return is **NaN.** Changing to **let sum = 0;** and the loop works fine. I thought - since javascript is dynamically typed, that the **sum += i;** would result in sum being a number by virtue of the assignment. I was wrong, but don’t understand why. Thanks.

---

<div class="post-metadata">

**Author:** ![northernocean](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@northernocean](https://forum.codewithmosh.com/u/northernocean)\
**Post date:** [July 4, 2022, 8:33pm UTC](https://forum.codewithmosh.com/t/exercise-8-sum-of-multiples-of-3-and-5/12167/5 "2022-07-04T20:33:49Z")

</div>

**let sum;** will initialize sum as _undefined_. If _sum_ is undefined, _sum += 1_ does not initialize sum to 0, then add 1 to it. It just adds 1 to sum, and _undefined + 1_ = _NaN_. So I think the main point is that the += operator does not do any initialized of variables.

for example:

```auto
sum += 1;
console.log(sum);

```

The above returns an error: “sum is not defined”. So you must declare sum, and since you intend to add numbers to it, you had best initialize it to 0 before you use it this way.

---

<div class="post-metadata">

**Author:** ![spiperman957](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@spiperman957](https://forum.codewithmosh.com/u/spiperman957)\
**Post date:** [July 4, 2022, 9:03pm UTC](https://forum.codewithmosh.com/t/exercise-8-sum-of-multiples-of-3-and-5/12167/6 "2022-07-04T21:03:42Z")

</div>

> [@northernocean](#):
>
> …the main point is that the += operator does not do any initialized of variables.

Thank you so much. I thought as much based on the result, but could not find a specific reference. New note to self - initialization never hurt. 🙂
