m0_70924714 2022-05-20 18:49 采纳率: 50%
浏览 85
已结题

Web前端开发JavaScript html

(1)计算每一个学生的总成绩,以表格形式输出
(2)按总成绩排名,以表格输出

img

img

  • 写回答

3条回答 默认 最新

  • Xiao_冬 2022-05-20 21:08
    关注

    img

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          table {
            border: 1px solid #000;
            margin-bottom: 20px;
          }
          table td,
          table th {
            border: 1px solid #000;
            text-align: center;
            width: 100px;
          }
        </style>
      </head>
      <body>
        <script>
          const list = [
            {
              stuNo: 1,
              course1: 77,
              course2: 66,
              course3: 88,
              total: "",
            },
            {
              stuNo: 2,
              course1: 78,
              course2: 89,
              course3: 79,
              total: "",
            },
            {
              stuNo: 3,
              course1: 92,
              course2: 84,
              course3: 78,
              total: "",
            },
            {
              stuNo: 4,
              course1: 78,
              course2: 99,
              course3: 83,
              total: "",
            },
            {
              stuNo: 5,
              course1: 85,
              course2: 97,
              course3: 69,
              total: "",
            },
          ];
          const totalList = list.map((item) => {
            const { course1, course2, course3 } = item;
            // 暂时以course1,course2,course3为准
            item.total = course1 + course2 + course3;
            return item;
          });
          const rankList = [...totalList];
          rankList.sort((a, b) => b.total - a.total);
    
          const str1 = totalList.reduce((pre, cur) => {
            const temp = `<tr>
                <td>${cur.stuNo}</td>
                <td>${cur.course1}</td>
                <td>${cur.course2}</td>
                <td>${cur.course3}</td>
                <td>${cur.total}</td>
              </tr>`;
            return pre + temp;
          }, "");
          const str2 = rankList.reduce((pre, cur) => {
            const temp = `<tr>
                <td>${cur.stuNo}</td>
                <td>${cur.course1}</td>
                <td>${cur.course2}</td>
                <td>${cur.course3}</td>
                <td>${cur.total}</td>
              </tr>`;
            return pre + temp;
          }, "");
    
          const tableTemplate = `<table><tr><th>stuNo</th><th>course1</th>
              <th>course2</th>
              <th>course3</th>
              <th>total</th></tr>`;
          document.body.innerHTML =
            `${tableTemplate}${str1}</table>` + `${tableTemplate}${str2}</table>`;
        </script>
      </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月21日
  • 已采纳回答 5月21日
  • 赞助了问题酬金5元 5月20日
  • 创建了问题 5月20日

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题