如何编写一个 javascript 函数,它接受一个带有姓名和分数的数组,并返回带有字母成绩和学生的数组? [英] How to write a javascript function that takes an array with names and scores, and returns array with letter grades and students?

查看:16
本文介绍了如何编写一个 javascript 函数,它接受一个带有姓名和分数的数组,并返回带有字母成绩和学生的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被困了好几天了.请帮忙!JavaScript 新手

I've been stuck for days. Please help! new to javascript

首先我绘制了学生的分数,并得到了一个仅包含分数的数组.然后我编写了一个 if/else 函数来获取学生分数并将其转换为字母成绩.但是我怎样才能得到这个字母等级的数组并列出所有获得每个等级的学生?然后在 es6 中将其写入 getStudentsByGrade const??

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));

给定一个带有姓名和分数的学生列表,编写一个函数 getStudentsByGrade,该函数接收一组学生和一组年级界限,并为您提供一个带有列表的对象年级和每个年级的学生姓名.

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.

输出应该是:

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

而且必须写在下面

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

给定:

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?    
}

推荐答案

您想将输入数组转换为按等级索引的对象 - 将数组转换为对象(或任何类型的单个值)的合适工具是减少.您可能会将将分数转换为字母等级的代码分离为它自己的函数,以获得更好的可读性.对于每个学生,获取他们适当的字母成绩,然后将该学生的姓名添加到累加器中的成绩数组中.如果累加器中尚不存在该等级,请先将其创建为新数组:

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));

这篇关于如何编写一个 javascript 函数,它接受一个带有姓名和分数的数组,并返回带有字母成绩和学生的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆