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.
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.
@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.
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.
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