Review JavaScript Fundamentals by Building a Gradebook App - Step 4

Tell us what’s happening:

Can’t pass this step.

Getting this error message: "Potential infinite loop detected on line " + e + “. Tests may be failing because of this.”

Not sure if I need to define totalScores & studentScore

Your code so far

function getAverage(scores) {
  let sum = 0;

  for (const score of scores) {
    sum += score;
  }

  return sum / scores.length;
}

function getGrade(score) {
  if (score === 100) {
    return "A++";
  } else if (score >= 90) {
    return "A";
  } else if (score >= 80) {
    return "B";
  } else if (score >= 70) {
    return "C";
  } else if (score >= 60) {
    return "D";
  } else {
    return "F";
  }
}

function hasPassingGrade(score) {
  return getGrade(score) !== "F";
}


// User Editable Region

function studentMsg(totalScores, studentScore) {
  

  if (studentScore >= 60) {return "Class average: " + getAverage + "Your grade: " + getGrade + "You passed the course."}
  else {return "Class average: " + getAverage + "Your grade: " + getGrade + "You failed the course."}
}
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));

// 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 4

You’re treating getAverage and getGrade as if they are variables. They are functions, so need function parentheses. You’ll also need to pass a value as an argument each time you call them.

The other issue is the punctuation/spacing in your strings.
This is a sample return, if you first correct the handling of your functions:
Class average: 71.7Your grade: FYou failed the course.

I’ve added the parenthesis and the spacing.

But what do you mean by “You’ll also need to pass a value as an argument each time you call them” ?

function studentMsg(totalScores, studentScore) {

if (studentScore >= 60) {return “Class average: " + getAverage() + " Your grade: " + getGrade() + " You passed the course.”}
else {return “Class average: " + getAverage() + " Your grade: " + getGrade() + " You failed the course.”}
}
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));

What would happen if you call this function without supplying an argument? You do this when you call getAverage() inside your studentMsg() function, for instance.

am i getting closer?

function studentMsg(totalScores, studentScore) {
  

  if (studentScore >= 60) {return "Class average: " + getAverage(scores) + " Your grade: " + getGrade(score) + " You passed the course."}
  else {return "Class average: " + getAverage(scores) + " Your grade: " + getGrade(score) + " You failed the course."}
}
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));

what is scores? where does it came from?
what is score? where does it came from?
what variables do you have available in this scope?

Function parameters can be confusing at first.

The function studentMsg() is defined with the parameters totalScores and studentScore.

When you call studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37), for example, the value of totalScores is that array and studentScore is 37.

Inside the studentMsg() function, you call two other functions, getAverage() and getGrade(). You’re going to call those using the parameters from studentMsg(), so if you call getGrade() with studentScore for instance, 37 will be passed to getGrade() as the value of its score parameter.

Does that make sense?

i got them from the top of the page:

function getAverage(scores)
function getGrade(score)

i can understand most of what you are saying, but still not enough to solve this task.

Although I’ve not finished this step, what he is referring to is the concept of “scope”. Check out ‘global’ and ‘local’ scope…that will tell you why he is asking where you found ‘scores’ and ‘score’ to be usable here.

I just read an article on “Global vs Local Scope”

Im guessing function getAverage(scores) & function getGrade(score) are Global Scope?

Yes.

You are super close.

The first thing you need to fix is that you cannot use scores or score here

score and scores are not defined in this function.

here is what you can use

once you update that, then you have missing punctuation in these strings here

there are small errors. but they are there.
triple check, character by character what the correct answer says here

Class average: average-goes-here. Your grade: grade-goes-here. You passed the course.
Class average: average-goes-here. Your grade: grade-goes-here. You failed the course.

you are missing punctuation.

once you fix that, then the test will pass

hope that helps

There are no global variables in this project.

Global variables are variables that can be accessed anywhere in your program.

So if I updated the project to include this variable, this is an example of a global variable.

You can run this code and see it show up in the console

function getAverage(scores) {
  console.log(myGlobalVariable)
  let sum = 0;

  for (const score of scores) {
    sum += score;
  }

  return sum / scores.length;
}

let myGlobalVariable = "this is an example"

you can use that variable anywhere on the page because it is was defined in the global scope.

scores is a parameter and can only be used in the getAverage function.
same goes for score down below

score is a parameter for that function.
it can only be used in that function.
you cannot use it outside of that function.
it is not available to you outside of that function.

You can look into this article on how to parameters works to get a better understanding

hope that helps

1 Like

thank you so much @jwilkins.oboe - i have now passed that step. I’ll also have a look at that article when i get a chance.

thank you also @igorgetmeabrain, @ilenia & @WarriorWebWorx

3 Likes

Congrats on completing the project.

1 Like