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.

1 Answer 1

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 ]
Sign up to request clarification or add additional context in comments.

4 Comments

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.
@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 and this.
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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.