(Sorry If my code looks dumb I have had no prior experience to Javascript. )
Error: I is not defined. Although I thought I defined “I” when I put “let I = 0”?
function getAverage(scores) {
for (let i = 0; i < scores; i++){
console.log(i);
}
return i;
}
I also thought that if it did work it would throw a NaN error since it might not treat the numbers in the scores array individually? I tried a “for of” loop I can show, that also had a definition error.
Your code so far
// User Editable Region
function getAverage(scores) {
for (let i = 0; i < scores; i++){
console.log(i);
}
return i;
}
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/124.0.0.0 Safari/537.36
Challenge Information:
Review JavaScript Fundamentals by Building a Gradebook App - Step 1
I’m not sure what your plan is exactly? Console logging i won’t compute the average. To get the average, you will need to get the sum of everything in the array.
I thought the "I++ 'would simply add 1 to I until the condition becomes false, that’s why I just thought If I logged I it would count from 1 all the way to 400 or whatever the total sum of scores was.
So would I have to mention every specific array number like, scores[0] + scores[1] inside the loop? I tried this code which separated all the numbers from the array:
function getAverage(scores) {
for (const score of scores) {
console.log(score);
}
for (let i = 0; i < score; i++){
console.log(i);
}
return scores;
}
But I got a reference error of scores not being defined.
I know how to add numbers, It’s just that I am trying to understand how to write the code for the sum? Would I instead add every value like: scores[0] + scores[1]… and put that information into a variable instead, and then simply divide the “sum” variable by scores.length? I am just confused how to code for a loop that would add each array value on it’s own? My for of loop separated the numbers but then I don’t know how to make the computer add them by itself, as I feel since it’s a loop I shouldn’t have to go in and manually do “49 + 50 + …” . I’ll redo the first lesson again, if you feel that I am missing critical information.
Ok, I have decided I should probably skip to the part where it talks about loops in the first lesson, but quick question, doesn’t “I++” in the third statement of the “for” loop do the adding?
The i++ adds 1 to i, but you aren’t doing anything that adds together the contents of the array.
You do seem to be a bit confused on what a loop does? All it does is repeat the code inside of the loop. You need to put the correct instructions inside of a loop to make it add together an array,s contents, which is why you need to know how to add together two numbers.
Would that mean that instead of I < scores it would be I = to a “sum” variable? If not I’ll see you tomorrow cause my brain’s already dead, and I’ll have to review.
Good Afternoon Jeremy! So this is my new and improved code. The only problem is that the console shows these two numbers: 9.2 and 4.5 vertical to one another which I am assuming is the result for each array, which means that unfortunately it is only dividing the first number of each array and not adding all of array values up first. Would that require a regular for loop?