Why am I wrong? [SOLVED]

Tell us what’s happening:

I would just like to understand why I’m incorrect. We are supposed to use .length, so that could be the issue. But the only thing they’re saying is incorrect is that total should equal twenty. I do get twenty. (By adding each value from the array) Did I find an alternative, or is it technically no twenty? When I console log, it shows each loop’s result. Could that be it?

Nevermind. The original solution is much more efficient. I was confused as to why we had to plug in “i” as the index for myArr. But then it was obvious that it would equal each value’s place/index in the array after each loop, and when addedto total, it keeps it to a single value, unlike with what I did. Sometimes I wonder what’s wrong with my brain lol

Your code so far


// Setup
var myArr = [ 2, 3, 4, 5, 6];

// Only change code below this line
var total = 0
for (total; total < 21; total += myArr[0]++) {
console.log(total);
}

Your browser information:

User Agent is: Mozilla/5.0 (Android 8.1.0; Tablet; rv:68.0) Gecko/68.0 Firefox/68.0.

Challenge: Iterate Through an Array with a For Loop

Link to the challenge:

Let’s make a small change to show you what’s going on.

var total = 0
for (total; total < 21; total += myArr[0]++) {
  console.log(total);
}

// I added these console logs to help debug
console.log(total);
console.log(myArr);

This code now logs

0
2
5
9
14
20
// Note: These are the new outputs I added
27
[ 8, 3, 4, 5, 6 ]

You are mutating myArr. It just so happens that the way you are mutating the array, you accidentally have the correct total at one point.

Let’s break down your loop syntax.

//   Initalize; Condition;  Increment
for (total;     total < 21; total += myArr[0]++) {

First the initalization:
Your initialization is not doing anything. You can see this by replacing it with nothing:

//   Initalize; Condition;  Increment
for (;          total < 21; total += myArr[0]++) {

This is a bad sign for a for loop.

Now the condition:
Your condition is hard coded for when total < 21. This is bad because a) you have to know the total in advance which makes this impossible to generalize for any array and b) you will stop when your total is bigger than 21.

Lastly the increment:
You are incrementing by adding the value of myArr[0] to the total and then increasing the value of myArr[0]. This is what total += myArr[0]++ means. You are not moving through the array. (You could use some pointer magic to make this work if we were in C/C++, but we’re not.)

This combo means that you are using the loop to

  1. Check if total < 21
  2. console.log the total
  3. increase the total by myArr[0] and increase the value of myArr[0]
  4. repeat

In the last two console.logs, you can see that this results in your total being incremented an extra time, to 27 and myArr being increased from its original value of 2 to a new value of 8.

All in all, I recommend you follow the format provided to get the total of the entires in myArr. This loop format you are trying will cause you all sorts of headaches.

1 Like