我最近问了这个问题,而且我的代码几乎就在那里。我正在创建一个函数,返回一个字符串,表示与给定分数对应的字母等级。
Notes:
* (100 - 90) --> 'A'
* (89 - 80) --> 'B'
* (79 - 70) --> 'C'
* (69 - 60) --> 'D'
* (59 - 0) --> 'F'
如果给定分数大于100或小于0,则应返回'无效分数'
如果得分在给定范围的0到2(含)之间,则返回带有' - '
如果分数在给定范围的8到9(含)之间,则返回带有' +'
没有F +,也没有F - 。
现在我有这个:
function convertScoreToGradeWithPlusAndMinus(score) {
var fscore;
switch(true) {
case (score <= 100 && score >= 90):
fscore = 'A';
break;
case (score <= 89 && score >= 80):
fscore = 'B';
break;
case (score <= 79 && score >= 70):
fscore = 'C';
break;
case (score <= 69 && score >= 60):
fscore = 'D';
break;
case (score <= 59 && score >= 0):
fscore = 'F';
break;
case (score > 100 && score < 0):
fscore = 'INVALID SCORE';
break;
default:
return 'INVALID SCORE';
}
var last_digit = score%10;
if(last_digit <=2 && last_digit >= 0 && score != 100){
fscore+='-';
}
else{
fscore+='+';
}
return fscore;
}
var output = convertScoreToGradeWithPlusAndMinus(91);
console.log(output); // --> 'A-'
此代码几乎就在那里,只是它向我展示了ff问题:
我真的被这个困住了,我想知道为了得到这个而我错过了什么。
请帮忙!
答案 0 :(得分:1)
您缺少else if
的第二部分。现在,如果您不添加-
,则会始终附加+
。
function convertScoreToGradeWithPlusAndMinus(score) {
var fscore;
switch(true) {
case (score <= 100 && score >= 90):
fscore = 'A';
break;
case (score <= 89 && score >= 80):
fscore = 'B';
break;
case (score <= 79 && score >= 70):
fscore = 'C';
break;
case (score <= 69 && score >= 60):
fscore = 'D';
break;
case (score <= 59 && score >= 0):
fscore = 'F';
break;
case (score > 100 && score < 0):
fscore = 'INVALID SCORE';
break;
default:
return 'INVALID SCORE';
}
if (score <= 100 && score >= 60) {
var last_digit = score%10;
if (last_digit <= 2 && score != 100) {
fscore += '-';
} else if (last_digit >= 8 || score == 100) {
fscore += '+';
}
}
return fscore;
}
console.log(convertScoreToGradeWithPlusAndMinus(91), 'A-');
console.log(convertScoreToGradeWithPlusAndMinus(94), 'A');
console.log(convertScoreToGradeWithPlusAndMinus(100), 'A+');
console.log(convertScoreToGradeWithPlusAndMinus(81), 'B-');
console.log(convertScoreToGradeWithPlusAndMinus(84), 'B');
console.log(convertScoreToGradeWithPlusAndMinus(88), 'B+');
console.log(convertScoreToGradeWithPlusAndMinus(71), 'C-');
console.log(convertScoreToGradeWithPlusAndMinus(74), 'C');
console.log(convertScoreToGradeWithPlusAndMinus(78), 'C+');
console.log(convertScoreToGradeWithPlusAndMinus(61), 'D-');
console.log(convertScoreToGradeWithPlusAndMinus(64), 'D');
console.log(convertScoreToGradeWithPlusAndMinus(68), 'D+');
console.log(convertScoreToGradeWithPlusAndMinus(31), 'F');
console.log(convertScoreToGradeWithPlusAndMinus(50), 'F');
console.log(convertScoreToGradeWithPlusAndMinus(59), 'F');
&#13;