Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

I’m confused by the question, I don’t even know where I went wrong.

Your code so far


// User Editable Region

function getAverage(scores) {
  let totalS = 0;
  let arrNum = [92, 88, 12, 77, 57, 100, 67, 38, 97, 89];
  
  for (let i = 0; arrNum.length; i++) {
    totalS + totalS + arrNum[i];
    let verage = totalS / arrNum.length; 
  }
  return verage;
}

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

You are not too far off but there are a few little issues which need addressing:

  1. You shouldn’t be declaring an array within the function and then using the function to manipulate its data. That is the purpose of function parameters. Your function should be acting upon the array which is passed to the function as scores (when the function is called). You need to refactor your code to remove all references to arrNum, replacing them with your function parameter.
  2. Your for loop needs a stopping condition, based on the value of i. You need to compare i to the length of the array.
  3. You declare verage inside your for loop, which makes it inaccessible outside of the loop (out of scope). This means that your return statement cannot find it. The easiest fix to this is to declare it alongside totalS, at the top of your function.

Your manipulation of the array items, to return an average value is fundamentally correct. You just need to tweak your code as above.

1 Like

How about this @igorgetmeabrain

  let arrNum = [92, 88, 12, 77, 57, 100, 67, 38, 97, 89];
  let verage = getAverage(scores);
function getAverage(scores) {
   let totalS = 0;
  
  for (let i = 0; i < arrNum.length; i++) {
    totalS + totalS + arrNum[i];
     verage = totalS / arrNum.length; 
  }
  return verage;
}

Why are you creating an array (arrNum) at all? An array will be passed to the function as scores when the function is called.

When I said to declare verage at the top of your function alongside totalS, I meant inside your function. Also, why are you declaring it as a function call?

Here’s an example of a function acting on an array, to find the largest value:

function findLargest(arr) {
  let largest = 0;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] > largest) {
      largest = arr[i];
    }
  }
  return largest;
}

console.log(findLargest([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) // 10
console.log(findLargest([20, 3, 15, 7, 101, 93])) // 101
  1. I don’t explicitly declare an array to be used inside the function. Instead, the parameter arr has an array passed to it when the function is called.
  2. The for loop declares i and increments it with each iteration of the loop, and has a stopping condition when i is no longer smaller than the length of the array.
  3. The variable largest is declared outside of the for loop, which means that it can be used within the for loop and also in the return statement (i.e. it is scoped to the function).

Does this make sense?

NO, I don’t understand. This is my first time coding in JavaScript, can you dumb it down for please!

You need to remove these lines of code here

That leaves you with this here

You need to remove all instances of arrNum and verage because we just got rid of that.

Once you fix that, then you need to fix two things.

The first thing to fix is how you are getting the total

This is incorrect here

So you need to update that

You should look at this article here

Pay close attention to the for loop in the first code example because that is what you need to change.

It will show you the correct way to get the total and show you what you need to update in that for to get it working properly.

Once you fix that for loop, then you need to fix the return statement.
Your return statement should be the average here

average = sum of all scores / total number of scores

So sum of all scores would be totalS in your case.
Then the tip they left you says you can use the length property to get the total number of scores.

It looks like you were trying to do that here

but you put it in the wrong place.
It shouldn’t be in the for loop.

Once you make those changes, then the test will pass

If this is your first time trying to code in JavaScript, then you’d be better to start at the beginning of the JavaScript course, with Learn Introductory Javascript by Building a Pyramid Generator, as you’ll need to learn the fundamentals before you can make any progress.

1 Like