Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

I seem to be struggling. I’m not sure whether to use a while loop or a for loop.

Your code so far


// User Editable Region

function getAverage(scores) {
  
}

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 (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 1

Hello;
You need two thing to solve this, as like lesson explain;

average = sum of all scores / total number of scores

the sum of all scores and the total number of scores. Also, there is no one solution to do this lesson. You can do it in different ways; just try.

But, if you don’t remember how to iterate with a loop or how to handle arrays, I highly recommend taking a look and reviewing what you learned before the lesson. Also, you can look at the documentation to remember how to create a loop or how to sum an array with a for loop. Because this lesson is more about reviewing what you learned before than teaching new things.
Happy coding…

I tried and I finally got it.

1 Like

Here’s my code:

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";
}

function studentMsg(totalScores, studentScore) {
  if (getGrade() !== "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([56, 23, 89, 42, 75, 11, 68, 34, 91, 19], 100));
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));

I tried, but I’m encountering a bug. I wonder what error I’m making.

Here you need to add a parameter. Find the correct paramater in your code block and assign it a value, more than or equal to passing marks.
@KoduFCC

You’ve implemented a control function to check if it’s passing or not.

So this function checks if the score is F or not, and if it’s not equal to that, guess what it will return. You can use this function to check if the score has passed or not, and of course, you should use the correct parameter for it. And yes your if (getGrade() !== "F") if statement doesn’t control anything without parametr.
Happy coding, you pass almost all review challenges, great! :ok_hand:

for step 4 you should have opened a new topic. Please keep the asking for help as one step per topic

I’ll continue on a different post.

I figured it out, and it worked.

2 Likes