Compute the sum of all integers that are multiples of 5, from 1 to 100

# computeSum(integers)

**n-kartha**#2

Have you tried writing code? If you can’t think of anything, here’s what you have to do:

Initalize a variable outside a `for`

loop.

In the loop, create a variable that starts at `5`

and ends at `100`

, stepping by `5`

as you go.

Inside the loop, add the number to the variable that you have initialized.

**ekunthomas**#3

Thanks for your prompt response, I tried as directed by you, see code below, but it is still failing. Regards. please help:

function computeSum(integers) {

var sum = 0;

```
for (var x = 5; x < integers.length; x += 5) {
if (x % 5 === 0 && x > 0) {
sum += x;
//console.log(x);
//console.log(sum);
}
}
return sum;
```

}

computeSum([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, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]);

**DanCouper**#4

Arrays are 0 indexed, you’re starting at 6, and you don’t seem to be getting the actual values from the array: either you take a number, in which case you don’t pass in an array, or you pass in an array, in which case you need to get the array values

**n-kartha**#5

You do not need a large array like you have made. All you need is the maximum number (cap) and it will work on its own.

Also, you do not need to check `x % 5 === 0 && x > 0`

because you’re starting at `x = 5`

(so it’s always greater than 0) already, and you’re incrementing by 5 (so you don’t have to check if `x`

is divisible by 5).

Try removing the `if`

and replace `[1, 2, 3, 4, ..., 100]`

with `100`

. Also, make a change to your `for`

loop, replacing `integers.length`

with `integers`

.

**n-kartha**#8

I know. There’s no need for him to do that. Looks like a newbie mistake. (I almost cringe.)

**n-kartha**#11

From context, I can assume that he’s not that experienced in computers, so I’d leave it at that and let him learn more before I give him a formula to do it lol

**ekunthomas**#13

n-Karta, pls help with the formula as I am still stuck, even after carrying out the correction please.

**n-kartha**#14

Okay. I’ll post the better solution then.

When you’re adding up multiples, it looks like this:

`5 + 10 + 15 + 20`

If you notice, the first and last numbers add up to `25`

. So do the 2nd numbers from the left and right, `10`

and `15`

. This is true for any set of multiples arranged in a correct order (although the sum may vary). Try it.

For instance, here are the first 6 multiples of `4`

.

`4, 8, 12, 16, 20, 24`

There’s a sort of symmetrical pattern here. The first and last numbers add up to a `28`

. The second and second last numbers add up to `28`

. So do the third and third last numbers.

This is where a pattern emerges. All you have to do is find `first number + last number`

and multiply it by `number of numbers / 2`

.

In your case, `(5 + max) * (max / 5) / 2`

. (`max / 5`

, since `max`

is the largest multiple you want, in your case, `100`

).

This is actually a play on arithmetic means, but I’ll let you figure that out on your own. Yes, this also works for odd numbers of multiples (if you divide an odd number by 2 you get a `.5`

which evens stuff out).

**ekunthomas**#15

Thanks for your valuable time, I am a newbie as discovered but wanting to learn. I read and comprehended your response but can not turn it into code that works, having tried over and over again. Please help.

**DanCouper**#16

If `computeSum`

takes a *number*, then if you remove `.length`

from your loop, and remove the `if`

block, and just do `sum += x`

, that will add every fifth number up to but not including the max number.

Alternatively, just do a calculation rather than looping over anything, as @n-kartha described.

If `computeSum`

takes an *array of numbers* then you need to access the array, so leave `.length`

there, start `x`

at 4, not 5, and `sum += integers[x]`

. Again, you don’t need the `if`

.

**ekunthomas**#17

I tried it like this(below) and it still return error of **Must be a number greater than 0**

function computeSum(integers){

var sum = 0;

for(var x = 4; x < integers.length; x+=5){

sum += integers[x] ;

//console.log(x);

//console.log(sum);

}

return sum;

}

**n-kartha**#18

Ugh. I’ll just give you the code for the loop.

```
function computeSum(n) { // renamed integers to n for brevity
let sum = 0;
for (let i = 5; i <= n; i++) { // start at 5, and loop until i <= n, instead of n.length
sum += i; // this is what I've been trying to tell you. Just add i.
}
return sum; // return the sum of numbers
}
```

You would use it like this:

```
computeSum(100);
```

**ekunthomas**#19

I tried it like you said(below) but it still complains of **Number must be greater than 0**.:

function computeSum(n) { // renamed integers to n for brevity

let sum = 0;

for (let i = 5; i <= n; i++) { // start at 5, and loop until i <= n, instead of n.length

sum += i; // this is what I’ve been trying to tell you. Just add i.

console.log(i);

console.log(sum);

}

return n

}

computeSum(100);

**DanCouper**#20

Where is this challenge you’re trying to do, because the error you’re getting makes no sense without some context?