Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

The problem I have occurs when I run the code. Before running it, the 3 correct averages appear in the console. When I run the code, 8 values appear instead of 3. I tried executing the code manually step by step, but I couldn’t figure out where the error is. Can you help?
let average=0;
let unshifted=0;
function getAverage(scores){
let i=scores.length-1;
while (i>=0){
unshifted=unshifted+scores[i];
i=i-1;
}
average=unshifted/scores.length;
console.log(average);
unshifted=0;
}

Your code so far


// User Editable Region

/* quesot codice funzionaconst dati=[45, 87, 98, 100, 86, 94, 67, 88, 94, 95];
let i=dati.length-1;
let unshifted=0;
function getAverage(scores) {
  while (i>=0){
    unshifted=unshifted+dati[i];
    i=i-1;
  }
  console.log(unshifted/dati.length);
}*/
/*let unshifted2=0;
let average2=0;
let sommaMedie=[];*/

let average=0;
let unshifted=0;
function getAverage(scores){
  let i=scores.length-1;
  while (i>=0){
    unshifted=unshifted+scores[i];
    i=i-1;
  }
  average=unshifted/scores.length;
  console.log(average);
  unshifted=0; 
 /*sommaMedie.push(average);
 i=sommaMedie.length-1;
  while(i>=0){
    unshifted2=unshifted2+sommaMedie[i];
    i=i-1;
  }
  average2=unshifted2/sommaMedie.length;
  console.log(average2);
  unshifted2=0;*/

 
/*let result=[];
for (const x of sommaMedie){
  result= result + x;
  console.log(result);
}*/
}


/*getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]);
getAverage([45, 87, 98, 100, 86, 94, 67, 88, 94, 95]);*/
getAverage([38, 99, 87, 100, 100, 100, 100, 100, 100, 100]);

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Hi. Your getAverage function must return a number. We should be able to see “return” in the body of the function before the calculation that produces the number that is the average score of the array.

I suggest the variables declared are within the function - local scope.
I got yours to work with a few tweaks.

Thank you! I just done it. If may I ask a question. Why is better declared variables within the function? Maybe because in this way if there’re problems you can check only the function?

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like