2017-05-13 11 views
-1

我正在处理switch语句,并在下面有一个小功能,将给定的数值分数转换为分数。或者至少这是它应该做的事情,但不知何故,这一切都出错了,我不知道为什么!Score to Grade转换器,或者:为什么我的switch语句错误? (Javascript)

function convertScoreToGrade(score) { 
var grade = ""; 
    switch(score) { 
case 100>=score && score>=90: grade = "A"; 
    break; 
case 89>=score && score>=80: grade = "B"; 
    break; 
case 79>=score && score>=70: grade = "C"; 
    break; 
case 69>=score && score>=60: grade = "D"; 
    break; 
case 59>=score && score>=0: grade = "F"; 
    break; 
case score>100 || score<0: grade = "INVALID SCORE"; 
} return grade; 
} 

convertScoreToGrade(10); 

例如,当我输入数字10时,我只会得到一个空字符串,这表明相关案例未被评估。任何帮助,将不胜感激。

+0

你的情况下,所有的结果在布尔值,而'score'不是一个布尔值,所以你永远不会得到匹配。你可以做'switch(true){...',但是'if/else'语句可能更好 –

+0

[documentation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/语句/开关)说* switch语句评估表达式,将表达式的值与case子句*匹配。 –

回答

-1

根据您的例子,这里是一个修改,使你的代码工作

大带走这里是你,你传递到switch语句中的参数相匹配。所以传递一个布尔值为true意味着如果你的条件是真的,情况就是这样。

IMO,switch语句是你应该用于这种情况。这是少量情况(5),并且对于稍后将处理或维护此代码的任何人都是非常易读的。

function convertScoreToGrade(score) { 
 

 
    // Check for invalid scores first 
 
    if(typeof score !== 'number' || score < 0 || score > 100) 
 
    return "INVALID SCORE"; 
 

 
    var grade = ""; 
 
    
 
    // Pass a boolean value, remember we are matching this value 
 
    // EX: (score < 90) is true when score is 0 - 89 
 
    switch(true) { 
 
    case score < 60: 
 
     grade = "F"; 
 
     break; 
 
    case score < 70: 
 
     grade = "D"; 
 
     break; 
 
    case score < 80: 
 
     grade = "C"; 
 
     break; 
 
    case score < 90: 
 
     grade = "B"; 
 
     break; 
 
    case score <= 100: 
 
     grade = "A"; 
 
     break; 
 
    } 
 
    
 
    // If you want partial grades 
 
    if(score % 10 <= 3 && score !== 100) 
 
    grade += "-"; 
 
    else if(score % 10 >= 7 || score == 100) 
 
    grade += "+"; 
 
    
 
    return grade; 
 
} 
 

 
// These are small test cases to show you 
 
// that convertScoreToGrade works as defined 
 
console.log(convertScoreToGrade(-1)); 
 
console.log(convertScoreToGrade(101)); 
 
console.log(convertScoreToGrade('The dog ate it.')); 
 

 
var i = 50; 
 
while(i <= 100){ 
 
    console.log(i, 'should be', convertScoreToGrade(i)); 
 
    i += 4; 
 
}

+0

这是一个很好的答案,非常感谢!正如你所说,我在上面的代码中为switch语句选择了错误的参数。 – user211309

+0

任何反对评论?我想获得有关错误的反馈。 @ user211309,如果你喜欢答案 - 你能否把它标记为正确的? – curtismorte

+0

当然 - 我想我刚刚做到了!我会upvote你的答案,但不能(但...)。 – user211309

0
function convertScoreToGrade(score) { 
    // scores is an array of objects 
    // Each element in the scores array has two properties, grade and score 
    // grade is the letter grade, score is the minimum score to achieve that grade 
    var i,l,scores = [{ grade: 'A', score: 90}, 
     {grade: 'B',score : 80}, 
     {grade: 'C',score: 70}, 
     {grade: 'D',score: 60 }]; 

    // Ensure score is between 0 and 100 inclusive 
    if (score < 0 || score > 100) { 
     return 'Invalid'; 
    } 
    // Loop through all the scores and exit when the score is larger than the minimum 
    l = scores.length; 
    for (i=0;i<l;i++) { 
     if (score >= scores[i].score) { 
      return scores[i].grade; 
     } 
    } 

    // If the score was not found, the grade is an F 
    return 'F'; 
} 

console.log(convertScoreToGrade(82)); 
console.log(convertScoreToGrade(90)); 
console.log(convertScoreToGrade(50)); 
+0

使用'for-in'时,不应该依赖枚举顺序。对于一个实施来说,改变它的顺序是完全有效的,这意味着“A”不一定是第一个。 –

+0

...并且你没有处理无效分数。 –

+0

@squint - 谢谢 – user2182349

0

,你可能想到的,坏的解决方案是比其他清楚差,但我已键入它,我认为这是有帮助的,你要明白,这是你将怎样if语句处理。请发布这样的糟糕的代码怜悯我。

function convertScoreToGrade(score) { 
var grade = ""; 
if(score>=0){ 
    grade = "F"; 
} 
if(score>=60){ 
    grade = "D"; 
} 
if(score>=70){ 
    grade = "C"; 
} 
if(score>=80){ 
    grade = "B"; 
} 
if(score>=90){ 
    grade = "A"; 
} 
if (score>100 || score<0){ 
    grade = "INVALID SCORE"; 
} 
return grade; 
} 

convertScoreToGrade(10);