javaScript算法扩展案例问题

依次输入小洪、小程、小黄、小吕、小庆、小兰、小紫的成绩(可自行设置0-100分),并使用冒泡排序进行排列,将排列好的成绩单以列表的形式显示在页面上,并将不及格的人员及成绩以红色字体显示

答案来自 梦想橡皮擦 狂飙组基于 GPT 编写的问答软件

function bubbleSort(scores) {
  for (let i = 0; i < scores.length; i++) {
    for (let j = 0; j < scores.length - i - 1; j++) {
      if (scores[j][1] > scores[j + 1][1]) {
        [scores[j], scores[j + 1]] = [scores[j + 1], scores[j]];
      }
    }
  }
  return scores;
}

function displayScores(scores) {
  let sortedScores = bubbleSort(scores);
  console.log("Sorted Scores:");
  for (let score of sortedScores) {
    let student = score[0];
    let scoreValue = score[1];
    if (scoreValue < 60) {
      console.log(`%c${student}: ${scoreValue}`, "color: red;");
    } else {
      console.log(`${student}: ${scoreValue}`);
    }
  }
}

let scores = [  ["小洪", 60],
  ["小程", 75],
  ["小黄", 55],
  ["小吕", 90],
  ["小庆", 65],
  ["小兰", 70],
  ["小紫", 80],
];

displayScores(scores);

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^