gpt4 book ai didi

javascript - 使用 javascript 进行成绩评估

转载 作者:行者123 更新时间:2023-11-28 03:02:12 24 4
gpt4 key购买 nike

我需要有关下面编写的代码的帮助。我想使用函数来运行它

var score = 100;
prompt ("Enter student score");


if (score <= 95 || score == 100) {
document.write (" A");
} else if (score <= 85 || score == 95){
document.write ("B");
} else if (score <= 75 || score == 85){
document.write ("C");
}else if (score <= 60 || score ==75){
document.write ("D");
}else if (score <= 50 || score == 60){
document.write ("E");
}else if (score <= 40 || score == 50){
document.write ("F");
}
else {
document.write (' Dropout.');
}

最佳答案

这个

if (score <= 95 || score == 100) {

无法按预期工作,因为它会检查值是否小于或等于 95 或等于 100

它不会检查之间的范围,例如 98 的分数。

除此之外,您还从 prompt 中得到了一个字符串,而不是一个数字。

<小时/>

您首先需要进行工作等级检查,可能是从等级的最大值到最小值进行检查(也许您需要使用 >= 进行检查)。

然后将代码包装在函数中并使用提前退出方法,在该方法中进行检查,函数直接返回值退出。

function getGrade(score) {
if (score > 95) return 'A';
if (score > 85) return 'B';
if (score > 75) return 'C';
if (score > 60) return 'D';
if (score > 50) return 'E';
if (score > 40) return 'F';
return 'Dropout.'
}

console.log(getGrade(+prompt ("Enter student score")));

关于javascript - 使用 javascript 进行成绩评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60893369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com