Iterating through an array - PLease Help

would someone PLEASE give me the solution to
https://csx.codesmith.io/units/precourse-part-2/challenge-for-loops-calculating-array-elements

I feel like an idiot! thanks

The link you posted takes me to a home page and not a challenge showing code. Plus, you need to make a first attempt at it and post your code. Then, we might be able to give you some hints to solve it own your own. We are not here to just give you solutions. Also, if you are having trouble with challenges on another site, you should do your self a favor and start working through the Free Code Camp curriculum instead.

2 Likes

Whoops. Thank you Randell. I completely forgot that I was accessing it through a login. My sincere mistake. I see why this looks bad lol.

I am indeed a member of freecodecamp as well… Trying many online resources to see what sticks… what I should learn first, etc…

I will post the actual challenge if that’s ok - I will make sure to not point to external stuff from now on!

Please help me with this guys…really simple why is my brain freezing??
CHALLENGE: You are given an array of five numbers called increaseByTwo . Use a for loop to iterate through the array and increase each number by two.

const increaseByTwo = [1, 2, 3, 4, 5];
// ADD CODE HERE

// Uncomment the line below to check your work!
// console.log(increaseByTwo); // -> should print [3, 4, 5, 6, 7];

What do you not understand about the instructions?

Do you know how to iterate through an array?

Do you know how to reference a specific element in an array?

I always get stuck with loops at first. Dunno why.

I know that arrays use zero based indexing. So the first item in an array is “0”, the second “1” etc. ie:

let friends = [‘George’, ‘Thai’, ‘Brandon’];
console.log(friends[2]); // should log: ‘Brandon’

So if you initialize a variable in your for loop and start it at 0 and and increment it by 1 each iteration as long as the variable’s value is less than the array’s length, you should be able to use that variable to reference any given element along the way and assign a new value to each.

const increaseByTwo = [1, 2, 3, 4, 5];
// ADD CODE HERE
let i = increaseByTwo.length

for (let i = 0 ; i < 5; i++) {
//not sure what to put here
}

// Uncomment the line below to check your work!
// console.log(increaseByTwo); // -> should print [3, 4, 5, 6, 7]; (for me it still prints 1,2,3,4,5)

The above line is not doing anything for you.

What if you wrote it like:

let arrLength = increaseByTwo.length

How could you use it in your for loop declaration (instead of hard coding the 5)?

If I asked you how to assign the number 10 to the element located at the 2nd index of the increaseByTwo array, how would you write that. If you figure that out, then solving the problem is just a matter of adding 2 to the value of the element at the ith index during each iteration of the array and reassigning the sum of the two values back to the same element (at the ith index).

Completly STUCK on this…i don’t know how to modify(i.e.assign number 10 to 2nd index)

other than “push” new values into a brand new array…???

if you can give me the solution i’d appreciate it…

(Thank G-d I only pass along work to coders for a living!!!)

When you are iterating through an array you are looping through its index. To get the index of an array you use square brackets like array[0]. So in your case you want to iterate increaseByTwo[i]. This is how you get the index but because you want to reassign the value at the given index you must reassign increaseByTwo[i] = increaseByTwo[i] + 2 or increaseByTwo[i] += 2

Also instead of declaring “i” you should just initialize in the loop, because the length of your array is 5 now but what if the length was greater than 5.

for(let i = 0; i < increaseByTwo.length; i++) {}

Sure you do. You already know how to reference an element by index (see below). Assignments are made right to left. The right side contains the value to be assigned and the left side will be assigned the right side’s value.

console.log(friends[2]); // should log: ‘Brandon’

I strongly suggest you review all of the FCC’s Basic JavaScript section which contains lessons on exactly what I am explaining to you here. One such lesson is below. I also suggest reviewing the lessons on for loops, because it seems you are not getting adequate instruction from the other learning sites.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/modify-array-data-with-indexes