1

in the for loop below I have arr[i] = x * i; I am basically trying to get multiples of numbers. the results of the code I have now is [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] I do not want the first element of the array to be 0 ..

var n = 10;
    var arr = [];
    var x = 2;
    for(var i = 0; i < n; i++ ){
        //arr[0] = x;
        arr[i] = x * i;
        // arr.push(x += x)
    }
    console.log(arr)

i want to be able to do arr[0] and see x. In this case that would be 2 (the number for the multiples..I don't know math words) [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

I know that the problem is that 2 * 0 is equal to 0 so arr[0] = 0. what is the best way to make it so that the first element will be the second loop. I was thinking about making an if statement. or using an array method that slices of the beginning of the array. I hope there is an easier way like changing the for loop.

jack blank
  • 5,073
  • 7
  • 41
  • 73

1 Answers1

4

There are two simple ways to fix this

  1. Change the loop starting value

    var arr = [],
        x = 2,
        n = 10;
    for (var i = 1; i <= n; i++) {    // Start with `1`
        arr.push(x * i);
    }
    
  2. Or, multiply with the next value of i,

    var arr = [],
        x = 2,
        n = 10;
    for (var i = 0; i < n; i++) {
        arr.push(x * (i + 1));        // Multiply with i + 1
    }
    

If you still want to solve it with array index assignment, then just remove the first element, with Array.prototype.slice after creating the entire array, like this

var n = 10,
    arr = [],
    x = 2;

for (var i = 0; i <= n; i++) {    // Note the limits, 0 to <= n
    arr[i] = x * i;
}

console.log(arr);
// [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ]

arr = arr.slice(1);

console.log(arr);
// [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ]
Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • i like your first one. didn't test the second one yet. I realize I could of used arr .push() but it felt natural to use arr[i] and i got undefined. I played around with the starting value like the way you had it before writing this question and i got undefined for the first el i guess my main problem is that i was stubborn in wanting to use arr[i] and not push(). can you give me a little reminder of why push is better so i would be more inclined to use the right method in the future? Thanks for your help. – jack blank May 27 '15 at 05:03
  • @user2537537 In the past, `push` has been shown to be very slightly faster than index assignment. Although, the main reason to use `push` would be to readability of the code I believe. You might want to check [this](http://stackoverflow.com/q/614126/1903116) and [this](http://stackoverflow.com/q/15649899/1903116). – thefourtheye May 27 '15 at 05:05
  • well I guess push just puts an element right after the last element into an array and doesn't care so much about the index i think. I don't want to be a burden but if you could show me how to get the solution with arr[i] (index assignment) that would be helpful. That's the way i was planning on solving it at the beginning and i want to see how much worse it would be if i went with index assignment. I will accept your answer either way. – jack blank May 27 '15 at 05:12
  • 1
    @user2537537 You can just remove the first element from the array with `slice`. I have shown that in the answer now. But don't prefer that. – thefourtheye May 27 '15 at 05:21