用JavaScript编写一个程序,记录学生成绩。要求集中输出60-69、70-79、80-89、90-100各个分数段学生的名字

用JavaScript编写一个程序,记录学生成绩。要求集中输出60-69、70-79、80-89、90-100各个分数段学生的名字

img


想要得到效果为:成绩在90-100的学生为xxx,xxx,xx
成绩在80-89间的学生为;xx,xxx

可以用filter直接做筛选。

题主要的代码如下,有帮助麻烦点个采纳【本回答右上角】,谢谢~~

img


<script>
    var students = [];
    for (var i = 0; i < 20; i++) {//随机20个学生成绩
        var score = Math.floor(Math.random() * 100);
        students.push({ name: '学生' + i, score: score });
    }
    document.write('<h1>59以下</h1>')
    document.write(students.filter(i => i.score < 60).map(i => i.name + "成绩为:" + i.score).join(' ') || '无记录')
    document.write('<h1>60-69</h1>')
    document.write(students.filter(i => i.score >= 60 && i.score <= 69).map(i => i.name + "成绩为:" + i.score).join(' ')||'无记录')
    document.write('<h1>70-79</h1>')
    document.write(students.filter(i => i.score >= 70 && i.score <= 79).map(i => i.name + "成绩为:" + i.score).join(' ') || '无记录')
    document.write('<h1>80-89</h1>')
    document.write(students.filter(i => i.score >= 80 && i.score <= 89).map(i => i.name + "成绩为:" + i.score).join(' ') || '无记录')
    document.write('<h1>90-100</h1>')
    document.write(students.filter(i => i.score >= 90).map(i => i.name + "成绩为:" + i.score).join(' ') || '无记录')
</script>

你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title> 页面名称 </title>
</head>
<body>

<script type="text/javascript">
var arr = [];
for (var i = 1; i <= 20; i++) {
    arr.push({ name: "学生名字"+i, score: Math.floor(Math.random()*101) });
}
console.log(arr);
var a = [], b = [],c = [], d = [], e = [];
arr.forEach(function(v, i){
    if (v.score>=90 && v.score<=100) {
        a.push(v.name);
    } else if (v.score>=80 && v.score<=89) {
        b.push(v.name);
    } else if (v.score>=70 && v.score<=79) {
        c.push(v.name);
    } else if (v.score>=60 && v.score<=69) {
        d.push(v.name);
    } else {
        e.push(v.name);
    }
});

console.log('90-100分数段学生的名字:',a);
console.log('80-89分数段学生的名字:',b);
console.log('70-79分数段学生的名字:',c);
console.log('60-69分数段学生的名字:',d);
console.log('60以下分数段学生的名字:',e);
</script>
</body>
</html>

判断一下就行。一个总数组,然后if判断就行。符合的就放到一个新数组中