给定一个函数 myGrading,我需要返回发送与给定分数对应的字母等级的文本。
(59 - 0) --> 'F'
基本上,如果给定分数大于 100 或小于 0,则应返回“INVALID SCORE”。
所以我所做的是我想出了一个使用 switch 语句的函数:
function myGrading(score) {
var gscore;
switch(true) {
case (score <= 100 && score >= 90):
gscore = 'A';
break;
case (score <= 89 && score >= 80):
gscore = 'B';
break;
case (score <= 79 && score >= 70):
gscore = 'C';
break;
case (score <= 69 && score >= 60):
gscore = 'D';
break;
case (score <= 59 && score >= 0):
gscore = 'F';
break;
case (score > 100 && score < 0):
gscore = 'INVALID SCORE';
break;
case (score <= 2 && score >= 0):
gscore += '-';
break;
case (score <= 9 && score >= 8):
gscore += '+';
break;
default:
return 'INVALID SCORE';
}
return gscore;
}
var output = convertScoreToGradeWithPlusAndMinus(91);
console.log(output); // --> MUST OUTPUT 'A-'
目前上面的代码没有显示正确的答案。知道我做错了什么吗?
我在研究如何解决这个问题时发现了这一点。抱歉,我知道这是一个旧线程,但我希望通过发帖,也许我可以得到一些关于我提出的解决方案的看法,看看它是否更好,如果不是,它的缺点是什么。这应该动态处理作为对象传递给它的任何分发类型。
// An object with the letter grade as key and the floor value of the grade as its value
const distribution = {
A: 90,
B: 80,
C: 70,
D: 60,
F: 0
};
const getGrade = (score, distribution, maxScore = 100, minScore = 0) => {
// Handle edge cases
if (score > maxScore || score < minScore) {
return "Error";
}
// Get an array of the letter grades
const grades = Object.keys(distribution);
// Sort the grades in descending order of the floor score of each grade
grades.sort((a, b) => distribution[b] - distribution[a]);
// Since the grades are sorted, the first grade to be lower than the score will be the correct grade to return
const grade = grades.find(grade => distribution[grade] <= score);
// No + or - for bottom grade
if (grade === grades[grades.length - 1]) {
return grade
} else if (score - distribution[grade] <= 2) { // check and return "-" grades
return grade += "-"
} else if (score - distribution[grade] >= 8) { // check and return "+" grades
return grade += "+"
} else { // return normal grades
return grade
}
};
如果您考虑范围
0-10
而不是 0-100
,您可以将逻辑简化为:
function myGrading (score) {
// Edge cases
if (score < 0 || score > 100) return 'INVALID SCORE';
if (score == 100) return 'A+';
// Important values
var decimal = score % 10;
score = Math.floor(score / 10); // <- here we reduce the range to 0-9
// 0 1 2 3 4 5 6 7 8 9
var scores = ['F', 'F', 'F', 'F', 'F', 'F', 'D', 'C', 'B', 'A'];
var grade = scores[score];
if (grade != 'F') {
if (decimal <= 2) grade += '-';
else if (decimal >= 8) grade += '+';
}
return grade;
}
希望有帮助。
我选择了类似于华盛顿的版本。检查边缘情况会变得很愚蠢。
let letter = ['F', 'F', 'F', 'F', 'F', 'F', 'D', 'C', 'B', 'A', 'A'];
let plumin = ['-', '-', '-', null, null, null, null, null, '+', '+'];
let major = num => letter[Math.floor(num / 10)];
let minor = num => num === 100 ? '+' : (plumin[num % 10] || '');
let grade = num => major(num) ? major(num) + minor(num): major(num);
console.log(grade(-10));
console.log(grade(0));
console.log(grade(1));
console.log(grade(25));
console.log(grade(65));
console.log(grade(77));
console.log(grade(88));
console.log(grade(95));
console.log(grade(100));
console.log(grade(110));
在这里尝试一下,这会将成绩从 for 改为 paragraf
<html>
<body>
<form action="#" method="GET" id="forma">
<input type="text" id="grade" name="grade"/>
<input type="submit" id="submit" value="PRESS" onclick="myGrading(this);"/>
</form>
<p id='grades'></p>
</body>
</html>
<script>
function myGrading() {
var gscore;
var score = document.getElementById('grade').value
switch(true) {
case (score <= 100 && score >= 90):
gscore = 'A';
break;
case (score <= 89 && score >= 80):
gscore = 'B';
break;
case (score <= 79 && score >= 70):
gscore = 'C';
break;
case (score <= 69 && score >= 60):
gscore = 'D';
break;
case (score <= 59 && score >= 0):
gscore = 'F';
break;
case (score > 100 && score < 0):
gscore = 'INVALID SCORE';
break;
case (score <= 2 && score >= 0):
gscore += '-';
break;
case (score <= 9 && score >= 8):
gscore += '+';
break;
default:
return 'INVALID SCORE';
}
document.getElementById('grades').innerHTML=gscore;
}
</script>
您需要采用逻辑将符号附加到开关盒之外,因为它们永远不会在那里执行。重构您的代码如下:
function myGrading(score) {
var gscore;
switch(true) {
case (score <= 100 && score >= 90):
gscore = 'A';
break;
case (score <= 89 && score >= 80):
gscore = 'B';
break;
case (score <= 79 && score >= 70):
gscore = 'C';
break;
case (score <= 69 && score >= 60):
gscore = 'D';
break;
case (score <= 59 && score >= 0):
gscore = 'F';
break;
case (score > 100 && score < 0):
gscore = 'INVALID SCORE';
break;
default:
return 'INVALID SCORE';
}
var last_digit = score%10;
if(last_digit <=2 && last_digit >= 0 && score != 100){
gscore+='-';
}
else if((last_digit <=9 && last_digit >= 7) || score == 100){
gscore+='+';
}
return gscore;
};
console.log(myGrading(91));
console.log(myGrading(75));
console.log(myGrading(100));
function convertScoreToGradeWithPlusAndMinus(score) {
var grade = ''; //creating a variable keeps it cleaner.
var scoreStr = score.toString(); //convert the score number into a string. This allows us to access indexes.
if(score > 100 || score < 0) return 'INVALID SCORE'; //set the edge case first.
if(score === 100) grade = 'A';
else if(score >= 90) grade = 'A';
else if(score >= 80) grade = 'B';
else if(score >= 70) grade = 'C';
else if(score >= 60) grade = 'D';
else if(score >= 0) grade = 'F';
if(grade !== 'F'){ //Negate 'F' before distributing + or - to letter grades.
if(scoreStr.substring(1) >= 8 || score === 100) {
grade += '+';
} else if(scoreStr.substring(1) <=2) {
grade += '-';
}
}
return grade;
}
var output = convertScoreToGradeWithPlusAndMinus(91);
console.log(output); // --> 'A-'
这就是我解决同样问题的方法。
function getScore(score) {
return
(score >=90 && score <=100) ? "A":
(score >=80 && score <=89) ? "B":
(score >=70 && score <=79) ? "C":
(score >=60 && score <=69) ? "D":
(score <=60 && score >=0) ? "E":
"Nota Inválida"
}
console.log(getScore(100))
You can try the following code for outputting the grades:
let mark = 97;
if (mark <= 100 && mark >= 90){
console.log("Gread A+")
}
else if(mark <= 89 && mark >= 80){
console.log("Gread A")
}
else if(mark <= 79 && mark >= 70){
console.log("Gread B")
}
else if(mark <= 69 && mark >= 60){
console.log("Gread C")
}
else if(mark < 60 ){
console.log("Gread F")
}
stackoverflow 中的完美 kavascript
let inPut =提示(“你的分数是多少?”,)
if (inPut <= 100 && inPut >= 90) {
alert('Your grade is A');
} else if (inPut <= 89 && inPut >= 70) {
alert('Your grade is B')
} else if (inPut <= 69 && inPut >= 60) {
alert('Your grade is C')
} else if (inPut <= 59 && inPut >= 50) {
alert('Your grade is D')
} else if (inPut <= 49 && inPut >= 0) {
alert('Your grade is F')
} else(
alert('Invalid Score')
);