我被困了好几天。请帮忙! javascript新手
首先,我绘制了学生分数,并得到了仅包含数字分数的数组。然后,我编写了一个if / else函数来获取学生分数并将其转换为字母等级。但是,我如何才能获得这一系列的字母成绩并列出所有获得每个成绩的学生呢?然后将其在es6中写入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
,该函数接受一组学生和一组成绩边界,并为您提供一个带有成绩列表的对象,每个年级的学生姓名。
输出应为:
{
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?
}
答案 0 :(得分:0)
您想将输入数组转换为按等级索引的对象-将数组转换为对象(或任何类型的单个值)的合适工具是reduce
。您可以分离出将分数变成字母等级的代码,以达到其自身的功能,以提高可读性。对于每个学生,获取其适当的字母等级,然后将该学生的姓名添加到累加器中的该等级数组中。如果累加器中尚不存在该成绩,请首先将其创建为新数组:
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));
答案 1 :(得分:0)
您想要这样的东西。借助Array.filter和Object.keys,您可以轻松地以指定格式获取结果。
const getStudentsByGrade = (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;
}
然后只需使用数据调用该函数即可查看结果:
let result = getStudentsByGrade(students, gradeBoundaries);
console.log(result);
这应该是结果输出:
{
"A":[
{
"name":"Dan",
"score":99
}
],
"B":[
],
"C":[
{
"name":"Daisy",
"score":65
},
{
"name":"Emily",
"score":77
}
],
"D":[
],
"E":[
{
"name":"Brendan",
"score":49
}
],
"F":[
]
}
答案 2 :(得分:0)
尝试一下:
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 = [];
function setScore (students) {
students.forEach(function (student, i) {
if (student.score >= 90) {
student["grade"] = "A";
} else if (student.score >= 80) {
student["grade"] = "B";
} else if (student.score >= 60) {
student["grade"] = "C";
} else if (student.score >= 50) {
student["grade"] = "D";
} else if (student.score >= 32) {
student["grade"] = "E";
} else {
student["grade"] = "F";
}
});
console.log('-----------------------NEW-------------------', students);
}