onTheInternet
onTheInternet

Reputation: 7253

How do I add numbers in an array

I am trying to work on a 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.

I have most of this done. However, I cant get the numbers to add up in the array. Here is the code I have so far

<script src="jquery-1.11.1.min.js"></script>
<script>
    $(document).ready(function () {
        var counter;
        for (counter = 0; counter < 1001; counter++) {
            if (counter % 3 == 0 || counter % 5 == 0) {
                var arrayNumbers = [];
                arrayNumbers.push(counter);


            }
        }

        var total = 0;
        $.each(arrayNumbers, function () {
            total += this;
        });

        console.log(total);
    });
</script>

The console logs this as 1000

I think what is wrng is the scope of my variables. How can I fix this?

Upvotes: 1

Views: 86

Answers (4)

Matias Cicero
Matias Cicero

Reputation: 26281

You should create you arrayNumbers outside the for loop, or it will be disposed and overriden in each loop.

<script src="jquery-1.11.1.min.js"></script>
<script>
    $(document).ready(function () {
        var counter;
        var arrayNumbers = [];
        for (counter = 0; counter < 1001; counter++) {
            if (counter % 3 == 0 || counter % 5 == 0) {
                arrayNumbers.push(counter);
            }
        }

        var total = 0;
        $.each(arrayNumbers, function () {
            total += this;
        });

        console.log(total);
    });
</script>

I did not want to mess up your algorithm, but I recommend you to use a counter variable for this exercise.

You should increment the counter by each multiple you find, and the final value of the counter would be the sum of all the multiples

<script src="jquery-1.11.1.min.js"></script>
<script>
    $(document).ready(function () {
        var counter = 0;
        var number;

        for (number = 0; number < 1001; number++) {
            if (number % 3 == 0 || number % 5 == 0) {
                counter += number;
            }
        }

        console.log(counter);
    });
</script>

Upvotes: 1

SivaDotRender
SivaDotRender

Reputation: 1651

Why don't you just do that summation in the for loop? kinda like this:

<script src="jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function () {
    var counter;
    var total= 0;
    for (counter = 0; counter < 1001; counter++) {
        if (counter % 3 == 0 || counter % 5 == 0) {
            var arrayNumbers = [];
            arrayNumbers.push(counter);
            total += counter;

        }
    }
    console.log(total);
});

Upvotes: 0

j08691
j08691

Reputation: 207901

You can do this without any array or jQuery:

var counter, total=0;
for (counter = 0; counter <= 1000; counter++) {
    if (counter % 3 == 0 || counter % 5 == 0) {
        total += counter;
    }
}
console.log(total);

jsFiddle example

Upvotes: 0

C M
C M

Reputation: 703

This can be achieved without array, by using loop only ones.

 $(document).ready(function () {
        var counter;
        var result;
        for (counter = 0; counter < 1001; counter++) {
            if (counter % 3 == 0 || counter % 5 == 0) {
               result+=counter;
            }
         console.log(result);
        }

Upvotes: 1

Related Questions