Review JavaScript Fundamentals by Building a Gradebook App - Step 4

Tell us what’s happening:

A mistake was cited in the console saying: score is not defined. I didn’t get the meaning of that. Which score is undefined and why?

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 (getGrade(Score) !== "F") {
    return "Class average:" + getAverage(totalScores) + "." + "Your grade:" +    (studentScore) + "." + "You passed the course.";
 } else {    
  return "Class average:" + getAverage(totalScores) + "." + "Your grade:" + (studentScore) +"." + "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/126.0.0.0 Safari/537.36 Edg/126.0.0.0

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 4

Your console says ReferenceError: Score is not defined, and this is the only place where you typed “Score”.

Score is in fact not defined.

You have done many mistakes:

  • The score is not defined bcause it is studentScore in your function parameter. Use StudentScore.
  • In your return statements, look after Your grade:, you have added studentScore. But you have to add grade from the getGrade function. Replace it with getGrade(studentScore).
  • Spacing is not proper in both the return statements. Ensure proper spacing after period and colon.

Hi there @haahinnawy !

Let me see if I can help you with this.

You should try:

  1. In your if statement you should test getGrade(studentScore) instead of getGrade(Score).
  2. Then you should replace studentScore for getGrade(studentScore) in your returns.

Note - we don’t need to repeat the same message with slightly different wording

Thank you. When I did as you said I got the correct message for a failed student as the student score in the challenge is F. Yet, I need the getGrade(score) for a message to the passing students. Here again I get the comment: score not defined.
The code now is:

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

Thus, the question remains why (score) in not defined?

Here’s the next place where you are using the undefined variable “Score”. You need to fix all places where you typed “Score”.

Sory, I am afraid there is a problem with language. I really don’t understand what it means to fix all places where the variable (score) is used.

You have typed ‘Score’ in multiple places. ‘Score’ isn’t a variable. Every single place you typed ‘Score’, you need to put the variable ‘studentScore’ instead

Sorry, i did that and the code didn’t pass. I have now reorganized my code to be:

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

I got a message for only who passed and the code didn’t pass.

Double check the spaces in the output here

// Correct code output 
Class average: 71.7. Your grade: F. You failed the course.

// Your code output 
Class average:71.7. Your grade: F. You passed the course.

Carefully see the spacing difference in both output.

Thank you. I corrected that space and the code passed. Thanks a lot.

1 Like

It’s my pleasure. Keep it up, happy Coding.

you ahould also check this, the output of hasPassingGrade is always different from “F”, as it returns a boolean value