DEV Community

Cover image for Grade Summary

Posted on

Grade Summary

Hi everyone!, I hope everyone is doing well during these troubling times! I ran into a particular problem, I thought would be fun to tackle and break down called Grade Summary. This function takes in a report(That happens to be a really long string) and what it returns is the average over every student including the total average of every student within an object. So lets get started with our IOCE.

Our input will be a string that will look like(not too sure why it would be input like this...):

'Jon 41 43 15 5 56\nJeremy 15 11 10 15 16\nJesse 19 17 20 19 18'

Our Output will be object where the keys are the student names and values their average. The object should also have a key named all assigned to the students' overall average, that should hopefully look similar to this.

    all: 15.9,
    Jon: 15.8,
    Jeremy: 13.4,
    Jesse: 18.6

Our Constraints will be that any calculations done, should be rounded to the tenth of a point.
Lastly there are no Edge Cases.

Since we know that our input is a string, and the output is an object with all of the student's average grade, we can declare and initialize a finalAverage object
and lets split by its line break(\n) from the string that was being inputted.
We're also going to need to a total average variable that we're going to use to keep track of the averages of the students.

const generateGradeSummary = (report) => {
  let finalAverage = {all: 0};
  let totalAverage = 0;
  let grades = report.split('\n');

Our currently grade will currently look like this:

    'Jon 41 43 15 5 56',
    'Jeremy 15 11 10 15 16',
    'Jesse 19 17 20 19 18'

Now that its in a form of an array we need to somehow separate the name from the string of numbers within the array. What we'd have to do to accomplish this, is to iterate through the array and split it once more by every empty space. Afterwards we're going to grab the average of each student by mapping the array from its second index and reducing the values into a single value and divide it by the number of scores that one particular student has.

The next step is to add all of our averages from each student and store them into our totalAverage variable in which we'll use to replace our all key with the correct value of the total average.

Our Final solution should look something similar to this:

const generateGradeSummary = (report) => {
  // create an finalObject, as the final output
    let finalAverage = {all: 0};
  let totalAverage = 0;
  // split the input by every line break  
  let grades = report.split('\n');
  // iterate through the grades
  grades.forEach(grade => {
    // split the array by every empty space 
    student = grade.split(' ');
    // to get the average of the students
    // map the student array starting from its seconds index
    // Convert string into number values and reduce to add them together, divided by the length of the student array
    let average = student.slice(1).map(i => Number(i)).reduce((a, b) => a + b, 0) / (student.length - 1);
    // add the students into the finalAverage object as the student's name as the key with the value of their average
    finalAverage[student[0]] = average;
  // grab each key value and add them together and store into totalAverage
  for (let key in finalAverage) {
    totalAverage += finalAverage[key];
  // change the all ley values into the totalAverage that was stored and divid by the number of keys
  // once done, round the value by tenths using toFixed(1);
  finalAverage.all = Number((totalAverage / (Object.keys(finalAverage).length - 1)).toFixed(1));
  // return the final output
  return finalAverage;

Thank you for the time to view over this post, and I hope it helps those that my have trouble if they've ever come across this problem, Thank you!
I hope everyone is staying safe until this annoying pandemic is over.

Top comments (0)