1

I try to develop a simple program that prints all numbers in between 1 and 100 that divide by 3 without any residual and calculate the total sum

I did it with for loop:

var sum = 0;
for (var i = 3; i <= 100; i = i + 3) {
    document.write("<br/>" + i);
    sum = sum + i;
}
document.write("<br/>sum = " + sum); //1683

But I failed when I wanted to do it with array:

var numbers = [];
var sum = 0;
for (var i = 0; i <= 100; i = i + 3) {
    numbers[i - 1] = i;
}

for (var index = 0; index < 100; index++) {
    document.write("<br/>" + numbers[index]);
    sum = sum + i;
}

document.write("<br/>sum = " + sum);
2
  • 4
    Three Issues that I see immediately: i - 1 will be -1 in the first iteration. Incrementing i by 3 and using it as array index will cause holes in the array. numbers won't have 100 elements, so you should not iterate over it from 0 to 100. What do you think i is in sum = sum + i; ? Commented Dec 23, 2016 at 7:04
  • why are you using two loops? stackoverflow.com/questions/41296674/array-loop-in-javascript/… try this Commented Dec 23, 2016 at 11:10

7 Answers 7

2

Use it like this,

Array indexes should start from 0, that is why I have introduced another variable j=0

var numbers = [];
var sum = 0;
for (var i = 0, j = 0; i <= 100; i = i + 3, ++j) {
    numbers[j] = i;
}

Update

First Issue: In your code, ie. below code of yours,

for (var i = 0; i <= 100; i = i + 3) {
    numbers[i - 1] = i;
}

In the first iteration,

i = 0;
numbers[0-1] = i // i.e numbers[-1] = 0; 

and in your second loop, you are starting the index from 0

for (var index = 0; index < 100; index++) {

Second issue:

Also, if you don't use a sequential counter to fill the Array, you will end with undefined values for the ones you did not fill.

enter image description here

If you notice, the output after the loop, it says numbers.length = 99 which is wrong it will not have that many items in it.

Third Issue:

In below code, even if you introduce a sequential counter, this is still wrong

for (var i = 0; i <= 100; i = i + 3) {
    numbers[i - 1] = i;
}

because i should start with 3 instead of 0, otherwise you will end up with 34 elements in the array because numbers[0] will be 0;

Fourth Issue:

In this code,

for (var index = 0; index < 100; index++) {
    document.write("<br/>" + numbers[index]);
    sum = sum + i;
}

You don't actually have to loop it till 100, you already have the numbers array filled, so you just need to use numbers.length, like this

var len = numbers.length;
for (var index = 0; index < len; index++) {
    document.write("<br/>" + numbers[index]);
    sum = sum + i;
}

A better way to write this

var numbers = [];
for (var i = 3, j=0; i <= 100; i = i + 3, j++) {
    numbers[j] = i;
}

var sum = numbers.reduce((a, b) => a+b);
console.log(sum);

The line var sum = numbers.reduce((a, b) => a+b); uses Array.reduce() method.

2
  • That's only one of the issues. Commented Dec 23, 2016 at 7:07
  • In the second loop, numbers array can have 33 values. why are you looping till 100 iterations? Commented Dec 23, 2016 at 7:12
1

adding number to array

var numbers = [];
for(var i = 3; i <= 100; i = i +3){
  numbers.push(i);
}

summation and printing values

var sum = 0;
for (var i = 0; i < numbers.length; i++) {
    document.write("<br/>" + numbers[i]);
    sum = sum + numbers[i];
}
document.write("<br/>sum = " + sum); //1683
1

There are few issues in your code.

for (var i = 0; i <= 100; i = i + 3) {
    numbers[i - 1] = i;
}

1: array is 0 based. so first insertion into the array goes for a toss.

2: the number array created will have skipping index like 3, 6 ,9

for (var index = 0; index < 100; index++) {
    document.write("<br/>" + numbers[index]);
    sum = sum + i;
}

3: Here you are iterating index till 100 , you should iterate it till the length of the numbers array only.

when index is 1,2 number[index] will become undefined.

4: sum = sum + i (i ??????)

You should try like this or you can also use push()

var numbers = [];
var sum = 0;
for (var i = 0,j=0; i <= 100; i = i + 3, j= j+1) {
    numbers[j] = i; // array is 0 based.
  
}



for (var index = 0; index < numbers.length; index++) {
    document.write("<br/>" + numbers[index]);
    sum = sum + numbers[index];
}

document.write("<br/>sum = " + sum);

0

Indexes in an array begin with zero.

for (var i = 0; i <= 100; i = i + 3) {
    numbers[i - 1] = i; // In the first iteration, there will be numbers[-1] = i;
}
0

You have several issues i suppose.

var numbers = [];
var sum = 0;
for (var i = 0; i <= 100; i = i + 3) {
    numbers.push(i);
}

for (var index = 0; index < numbers.length; index++) {
    document.write("<br/>" + numbers[index]);
    sum = sum + i;
}

document.write("<br/>sum = " + sum);

Also for array you can use:

for (var i in array) {
console.log(array[i]);
}

And I'm pretty sure, that array of number sequence is absolutely useless, if there is no other information in it.

2
  • "You have several issues i suppose." Maybe you can explain what they are? Commented Dec 23, 2016 at 7:06
  • See my code changes. numbers[i-1] -> numbers[-1] in first iteration which doesn't exist. And first for loop has 0-100 iterations and second has only 0-99.
    – MadDocNC
    Commented Dec 23, 2016 at 7:08
0

Try this

var numbers = [];
var sum = 0;
for (var i = 0; i <= 100; i = i + 3) {
    numbers[(i-3)/3] = i;
}

for (var index = 0; index <  numbers.length; index++) {
    document.write("<br/>" + numbers[index]);
    sum = sum + numbers[index];
}

document.write("<br/>sum = " + sum);

Here is the fiddle i tried https://jsfiddle.net/4ncgnd7c/

0

This should work using a single loop

var numbers = [];
var sum = 0;
for (var i = 3; i <= 100; i = i + 3) {
    numbers[i] = i;
    document.write("<br/>" + i);
    sum = sum + i;
}

document.write("<br/>sum = " + sum);

Not the answer you're looking for? Browse other questions tagged or ask your own question.