Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

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 student score and convert it to a letter grade. But how can I take this array of letter grade and list out all the students that got each grade? and then write this in es6 into the getStudentsByGrade const??

 var studentScores = students.map(function (student) {
   return student.score;
 })
 console.log(studentScores);

 function toLetterGrade(studentScores) {
   var textG = '';
   var result = [];
   for (i = 0; i < studentScores.length; i++) {
     textG = '';
     if (studentScores[i] >= 90) {
       textG = "A";
     } else if (studentScores[i] >= 80) {
       textG = "B";
     } else if (studentScores[i] >= 60) {
       textG = "C";
     } else if (studentScores[i] >= 50) {
       textG = "D";
     } else if (studentScores[i] >= 32) {
       textG = "E";
     } else {
       textG = "F";
     }
     result.push(textG);
   }
   return result;
 }

 console.log(toLetterGrade(studentScores));

Given a list of students with a name and a score, write a function getStudentsByGrade that takes in an array of students and a set of grade boundaries and gives you an gives you an object with a list of grades and the names of students for each grade.

the output should be:

{
  A: ['Dan'],
  C: ['Emily', 'Daisy'],
  E: ['Brendan']
}

And must be written inside the following

const getStudentsByGrade = (students, gradeBoundaries) => {    
  // solution in here    
}

Given:

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

const gradeBoundaries = {A: 90, B: 80, C: 60, D: 50, E: 32, F: 0};

const getStudentsByGrade = (students, gradeBoundaries) => {    
  // How do I do this?    
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

const getStudentsByGrade = (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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...