Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

Please I need help here.. my code is not working. I don’t understand by my average should return a number.

Your code so far


// User Editable Region

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

}

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 (Linux; Android 12; CPH2387) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.98 Mobile Safari/537.36

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Hello,

Your code seems to have two errors. I would check on the declaration of the sum variable and also on which score element is being added to the variable.

Happy coding.

I don’t understand what you mean please explain better

Hello,

In this example, before we can use a variable, we need to declare it and assign it an initial value.

Example – Declaring and initializing a variable:

const a = 1;
let b = 2;

There are important differences to keep in mind when choosing how to declare your variables.

Since we are working with a primitive type ,If we try to re-assign a with a new value
ex

a +=5  //This will produce an error. As a has being declared with const 
       //it becomes read-only. 

However, if we try the same with b, it works:

b += 5;  // 'b' is now 7

Because b was declared with let, it’s allowed to be reassigned, unlike const, which makes the variable read-only after its initial assignment.

Now for the second part:
In JavaScript, when working with arrays, you can access individual elements using their index. An index is simply a number that represents the position of an element within the array.

Here’s a simple example:

const arr = [1, 2, 3, 4, 5];
console.log(arr[0]); // Outputs: 1

Important: Arrays in JavaScript are zero-indexed.
This means the first element is at index 0, the second at index 1, and so on.

Ok let me put that into perspective and come back to you.. thanks

The code still didn’t work.. I tried it keeps telling me my getAverage function should return a number

Hi there. Post your latest code here in your next reply. Use three (```) back ticks on a separate line before and after your code.

Ok I will do just that.. please I need clearity thanks

@Josh2nice Don’t post screenshot here. Also your aren’t need the back ticks around the code in the challenge editor. You need it here in your reply.
Copy and paste your full code here in your next reply and add three back ticks on a separate line before and after your code block to preform it.

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

}

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]));

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Tell us what’s happening:

My code seems not to be working.. I have tried different code but still not functioning.. I need help

Your code so far



// User Editable Region

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

}

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 (Linux; Android 12; CPH2387) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.98 Mobile Safari/537.36

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

You need to declare a variable before using it, also you don’t need to declare scores becouse that is the parameter of the function

consider whar your function does

for each element in scores does:

  • add scores[i] to sum (where does sum come from? it did not exist before this line)
  • set sum to 0 (so the sum on the line before does not matter)
  • set scores to empty array (what about all the other elements in scores? have we lost them?)

Then after the array

  • assign to average the value of sumTotal / scores.length (what is sumTotal? it is the first time it appear here)

please do not create multiple topics for the same step, for this time I am merging your two topics, but please keep it in mind