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
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
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!
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];
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ā
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)
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++) {}