Help with accessing an arrays elements

Hello, I’m on the credit card checker project, and I need help accessing an arrays like so

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 
// How do I access every other element starting from two? 
//I want my output to be [2, 4, 6, 8, 10]; 

Thanks in advance!

1 Like

Hey @hiddenkiller47349932 from your post I think this is the right answer:

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 

for(let i = 1; i < nums.length; i+=2) {
  console.log(nums[i]);
}

The output is:

[2, 4, 6, 8, 10]

What’s going on is that it starts from position 1 in the array which is the number 2 (position 0 is the number 1) and it goes all the way up to but not including nums.length, but it increments by two hence i+=2.

Thanks this helped a lot!

1 Like

@adriansomor, do you know the credit card project? link. Given this is the project hiddenkiller47349932 is on

although adriansomor solution works, its not the ideal solution for this project, given we need all the numbers, but multiple the even numbers. So using the modulo operator to determine if an index is even or odd, is the better approach for this scenario.

2 Likes