Skip to content Skip to sidebar Skip to footer

How To Write A Javascript Function That Takes An Array With Names And Scores, And Returns Array With Letter Grades And Students?

I've been stuck for days. Please help! new to javascript first I mapped the students scores, and got an array of just the number scores. Then I wrote a if/else function to take the

Solution 1:

You want to transform an input array into an object indexed by grade - the appropriate tool to transform an array into an object (or any sort of single value) is reduce. You might separate out the code that turns the score into a letter grade into its own function, for better readability. For each student, get their appropriate letter grade, and then add that student's name to that grade array in the accumulator. If that grade doesn't exist yet in the accumulator, create it as a new array first:

constmakeGetGrade = gradeBoundaries => {
  const gradeBoundaryArr = Object.entries(gradeBoundaries)
    .map(([grade, cutoff]) => ({ grade, cutoff }))
    .sort((a, b) => b.cutoff - a.cutoff);
  returnfindGrade => gradeBoundaryArr
    .find(({ grade, cutoff }) => findGrade > cutoff)
    .grade;
};

constgetStudentsByGrade = (students, gradeBoundaries) => {
  const getGrade = makeGetGrade(gradeBoundaries);
  return students.reduce((a, { name, score }) => {
    const grade = getGrade(score);
    if (!a[grade]) a[grade] = [];
    a[grade].push(name);
    return a;
  }, {});
};


const students=[{name:'Daisy',score:65,},{name:'Dan',score:99,},{name:'Emily',score:77},{name:'Brendan',score:49}];

const possibleGradeBoundaries = {
  A: 90,
  B: 80,
  C: 60,
  D: 50,
  E: 32,
  F: 0,
};
console.log(getStudentsByGrade(students, possibleGradeBoundaries));

Solution 2:

Try this:

const students = [{

name: 'Daisy',

score: 65,

}, {

name: 'Dan',

score: 99,

}, {

name: 'Emily',

score: 77

}, {

name: 'Brendan',

score: 49

}];

console.log('-----------------------OLD-------------------', students);
setScore(students);
var result = [];
functionsetScore (students) {
  students.forEach(function (student, i) {
    if (student.score >= 90) {
       student["grade"] = "A";
     } elseif (student.score >= 80) {
       student["grade"] = "B";
     } elseif (student.score >= 60) {
       student["grade"] = "C";
     } elseif (student.score >= 50) {
       student["grade"] = "D";
     } elseif (student.score >= 32) {
       student["grade"] = "E";
     } else {
       student["grade"] = "F";
     }
  });
  console.log('-----------------------NEW-------------------', students);
}

Solution 3:

You would want something like this. With the help of Array.filter and Object.keys, you can easily get your result in the specified format.

constgetStudentsByGrade = (students, gradeBoundaries) => {
  let result = {};
  let letterGrades = Object.keys(gradeBoundaries);

  letterGrades.forEach((letterGrade, index) => {
    result[letterGrade] = students.filter(student => {
      if (student.score >= gradeBoundaries[letterGrade]) {
        if (index > 0) {
          let higherLetterGrade = letterGrades[index - 1];
          if (student.score < gradeBoundaries[higherLetterGrade]) {
            return student.name;
          }
        } else {
          return student.name;
        }
      }
    });
  });

  return result;
}

Then simply invoke the function with data to see the result:

let result= getStudentsByGrade(students, gradeBoundaries);
console.log(result);

This should be the result output:

{
  "A":[
    {
      "name":"Dan",
      "score":99
    }
  ],
  "B":[

  ],
  "C":[
    {
      "name":"Daisy",
      "score":65
    },
    {
      "name":"Emily",
      "score":77
    }
  ],
  "D":[

  ],
  "E":[
    {
      "name":"Brendan",
      "score":49
    }
  ],
  "F":[

  ]
}

Post a Comment for "How To Write A Javascript Function That Takes An Array With Names And Scores, And Returns Array With Letter Grades And Students?"