Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

In regard to the For, Of function and before I start my answer - I looked at w3schools. I’m just trying things to learn, but from their For, Of example code they had “br” rather than “\n” (like in the FCC pyramid code).
Should I ever use the br code instead of the new line code???
When I changed theirs to “\n” it didn’t work, maybe just a bug their end?
Here’s their code:

let language = "JavaScript";
let text = "";
for (let x of language) {
  text += x + "<br>"; 
}

hope ok, thanks

Your code so far


// User Editable Region

/* step 41: iterates over each item in an iterable object (iterable) and temporarily assigns it to a variable (value). iterate through your array, assigning each value to a variable (value).
  for (const value of iterable) {} */
/* step 42: you used the addition operator + to increase the value of i by 1. Do a SIMILAR thing with a string value, by APPENDING a new string to an existing string. Use the addition operator to concatenate the (value) to the result value. */
/* step 43: result = result + value; */
/* step 22: make use of the .length property of an array - this RETURNS the NUMBER OF elements in the array. */
/* Example Code: average = sum of all scores / total number of scores. 
You can use the length property to get the total number of scores. */

function getAverage(scores) { 
/* ??? sum = 0;

??? return sum / scores.push();*/
}

console.log(getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]));
console.log(getAverage([45, 87, 98, 100, 86, 94, 67, 88, 94, 95]));

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Hi there!

The example code won’t work for the challenge step. You need to itirates over the scores (the function parameter), using for...loop.

Some questions on the whole function overall:
The function is getting called with the console.log and this starts the function whereby it places the numbers array into the function parameter to start with, then…

A query on the loop iterator (controls how your loop goes through your logic, a starting point):
for (let i = 0; i < scores.length; i++)
It’s saying that i is the CONTROLLER and it’s going to do the hard work. It’s beginning at 0 in the array, being 92?
Then the condition:
As long as the iterator that is working it’s way through the array is less than the array contents length it will keep looping?
Then the iteration:
As long as i is looping through, it will also be adding up the array but it won’t concatenate as the array is numbers?

A second query, on the total:
total += scores[i];
So it’s saying that once the loop is not less than the length, then it will replace the function getAverage parameter, being scores, the final counted TOTAL?
Does this mean the scores parameter is holding 2 values, the name of SCORES plus it’s TOTAL as well? Is that why the function can still read scores.length? Or, how does the function read scores.length if the scores parameter has been replaced by total??

A third query on the i=0:
Why does it see it as a numeric, rather than a falsey? Can an iterator ever be a boolean?

Hope i make sense, as a beginner!
thanks

the starting value of i is 0, it is not linked to the array until you write scores[i], and scores[0] is 92 for that function call

what do you mean with this? I don’t understand your sentence

in the line total += scores[i] the variable that is being assigned a value is total
it is a compound assignment, the extended version is total = total + scores[i], the previous value of total is summed to scores[i] and then assigned back to total. the value of scores here doesn’t change.
The value of scores[i] depends on which iteration of the loop is, and it will have one at a time the value of each item in the loop

because a numeric i is the easy way to have the i take value of each index of the array.
An iterator can be a boolean, though I can’t think of a situation in which a while loop wouldn’t be better

you can put your code here: Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code and see it executed step by step, it’s a nifty tool that help with understanding code flow

that’s a great response, i understand heaps better now, many thanks!

My sentence that was not understandable was because i thought addition was for numbers and concatenation was for strings but I checked and found that concatenating is the word for adding, and so for numbers we use an addition symbol TO concatenate. Silly me!.

Also, when i got to step 2, the function for step 1 was different in the answer, it used For, If…was that just to show we could do two different ways of the same question? If so, are there other ways of doing the averages function or is it only and always a loop variety?

thanks,

the + operator is the addition operator for numbers, 3 + 2 gives 5, and the concatenation operator for strings, 'h' + 'i' is 'hi`

when you go to the next step, your code is not kept in memory, but a default starting code for the next step is used, that doesn’t mean that your code is wrong, it’s just that it wasn’t the way saved as solution for this project.

There are many ways to calculate an average, and some of them don’t use loops