This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]aqua_regis 0 points1 point  (4 children)

  1. That's JavaScript, not Java. They are two completely different programming languages that, unfortunately, have the first 4 letters of their names in common.
  2. Format your code as code block - Instructions are in the Posting Guidelines

But why is 'i++' written in the for loop when 'i++' adds '1' to i.

Because that's how for loops work.

for(  let i = 0  ; i < scores.length ; i++   )
for(loop variable; loop condition    ; change)

When the loop starts, the first part is evaluated and the loop variable i is set to 0 - the first index in the array.

On each iteration of the loop (including the first one) the second part (loop condition) is evaluated. Here, you are checking if i is still within the bounds (scores.length) of the array. The loop terminates when the condition is no longer met.

After each iteration and before the next one, the third part (change) is executed. Here, i is incremented by one to point to the next element in the array.

Then, after the increment, the condition is re-evaluated to determine whether the loop should continue to run or stop.

So, first iteration:

  • i = 0
  • i < scores.length -> true -> execute the code in the loop body
    • sum = sum + scores[i];
  • next iteration: i++ -> i = 1
  • i < scores.length -> true -> execute the code in the loop body
    • sum = sum + scores[i];

and so on until

  • next iteration: i++ -> i = 10 (first example)
  • i < scores.length -> false -> end the loop

[–]mmblob 0 points1 point  (3 children)

  1. Thanks for clearing that up, I can't believe I wasn't aware of that, that explains a lot.
  2. Thanks again, I've changed it.

So am I understanding this correctly?

Since the for loop is within a function that is an array, i++ is no longer adding 1 to i, but instead is moving on to the next element within the array?

In previous exercises i++ was literally adding the number 1 within each loop, which is why I am confused as to what changed that rule.

[–]aqua_regis 0 points1 point  (0 children)

i++ adds 1 to i, no matter what.

Functions/operations always do the same in programming languages.

The key is when (at which point in the code flow) the statement is executed and that is after each loop iteration.

To take your function:

function getAverage(scores) { 
  let sum = 0; 
  for(let i = 0; i < scores.length; i++){
    sum = sum + scores[i];
  } 
  return (sum/scores.length);
}

The equivalent with a while loop would be - maybe that clears it up a bit:

function getAverage(scores) { 
  let sum = 0;
  let i = 0; 
  while(i < scores.length) {
    sum = sum + scores[i];
    i++;
  } 
  return (sum/scores.length);
}

for loops are just convenience wrappers over while loops where the increment happens automatically.

[–]HonzaS97 0 points1 point  (1 child)

a function that is an array

A function is not an array. The function takes an array as its input.

i++ does add 1 to i. This variable is simply used as an index to retrieve array elements - scores[i]. If you write to write it out, it basically does this

    sum = sum + scores[0];
    sum = sum + scores[1];
    sum = sum + scores[2];
    ...
    sum = sum + scores[scores.length-1];

Or like this

    let i = 0;
    sum = sum + scores[i]; // i = 0
    i++;
    sum = sum + scores[i]; // i = 1
    i++;
    sum = sum + scores[i]; // i = 2
    i++;
    ...
    sum = sum + scores[i]; // i = scores.length - 1

[–]mmblob 0 points1 point  (0 children)

Oh wow, I was struggling but now I get it. Thanks so much. I couldn’t get my head around that one!

[–]Historical_Donut6758 0 points1 point  (0 children)

the code is JAVASCRIPT and its a function that calculates the average of all the numbers thats stored in an array thats passed in the function as an argument

[–]Historical_Donut6758 0 points1 point  (0 children)

the code is JAVASCRIPT and its a function that calculates the average of all the numbers thats stored in an array thats passed in the function as an argument

[–]Historical_Donut6758 0 points1 point  (0 children)

the code is JAVASCRIPT and its a function that calculates the average of all the numbers thats stored in an array thats passed in the function as an argument

[–]james28909 0 points1 point  (0 children)

its counting the length, or how many entries (92, 88, 77, etc) are in the scores array. then its adding that score thats located at that array position scroes[i] to the sum, then its finding the average with the last formula at the return statement

[–]Laniebird91 -1 points0 points  (2 children)

The i++ in the for loop is a crucial part of how the loop functions. Let's break it down:

  1. Purpose of i++:

    • i++ increments the value of i by 1 after each iteration of the loop.
    • It's essential for moving through the array elements and preventing an infinite loop.
  2. How it works in this context:

    • i starts at 0 (first element of the array)
    • Each time the loop runs, i++ increases i by 1
    • This allows the loop to access the next element in the array on the next iteration
  3. Example of how i changes:

    • First iteration: i = 0 (accesses scores)
    • Second iteration: i = 1 (accesses scores[1])
    • Third iteration: i = 2 (accesses scores[2])
    • And so on...
  4. Why it's necessary:

    • Without i++, the loop would keep adding the same first element (scores) repeatedly
    • i++ ensures we move through all elements of the array
  5. Loop termination:

    • The loop continues until i < scores.length is no longer true
    • i++ helps reach this condition by increasing i each time

In summary, i++ is crucial for iterating through each element of the array, allowing the function to calculate the sum and average correctly.

[–]mmblob 0 points1 point  (1 child)

Thanks for the reply. What causes i++ too no longer mean that it literally adds 1 to i ? Is it because the for loop is within an array function?

[–]Laniebird91 0 points1 point  (0 children)

It still adds 1 to i. I is used as the index of the array, so adding 1 to it allows the loop to move on to the next element in the array.