如何在函数中的 switch case 中分配新值?

问题描述 投票:0回答:4

目前正在编写一个将数字成绩转换为美国系统的函数。基本上结果应该是这样的:

您获得了 D (60%)!

但是我明白了:

你得了60 60%!

除了括号之外,我应该怎样做才能让它看起来尽可能像?

代码如下:

function gradeConverting(grade) {
    let gradePercent = grade + "%";
    switch (grade) {
        case (90 < grade && grade <= 100):
            grade = "A";
            break;
        case (80 < grade && grade <= 89):
            grade = "B";
            break;
        case (70 < grade && grade <= 79):
            grade = "C";
            break;
        case (60 <= grade && grade <= 69):
            grade = "D";
            break;
        case (50 <= grade && grade <= 59):
            grade = "E";
            break;
        case (grade <= 49):
            grade = "F";
            break;
    }
    return console.log("You got a " + grade + " " + gradePercent + "!");
}

gradeConverting(55);
javascript syntax switch-statement
4个回答
8
投票

您的代码逻辑完全有效,但您使用

switch()
的方式是错误的。

查看文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

如果您将

switch(grade)
放在
something
中,则
case something
预计会匹配
value
grade
,而不是返回
expression
true|false

例如:

switch (grade) {
        case 90: // only for grade === 90
            grade = "A";
            break;
        case 55: // only for grade === 55
        ...

事实上,您可以拥有一个包含多个

if
语句的函数,然后返回
lettergrade
。 或者仍然使用当前的逻辑进行一些修改并仍然使用
switch()

我为

lettergrade
创建了另一个变量,建议不要修改
grade
,除非你知道自己要做什么。

function gradeConverting(grade) {
    let gradePercent = grade + "%";
    var lettergrade = ""; 
    switch (true) { // change to true 
        case (90 <= grade && grade <= 100):
        // you want (90 <= grade && grade <= 100) to be evaluated as true in order to execuate
            lettergrade = "A";
            break;
        case (80 <= grade && grade <= 89):
            lettergrade = "B";
            break;
        case (70 <= grade && grade <= 79):
            lettergrade = "C";
            break;
        case (60 <= grade && grade <= 69):
            lettergrade = "D";
            break;
        case (50 <= grade && grade <= 59):
            lettergrade = "E";
            break;
        case (grade <= 49):
            lettergrade = "F";
            break;
    }
    return console.log("You got a " + lettergrade + " (" + gradePercent + ")!");
}

gradeConverting(100);


2
投票

您没有正确使用

switch
语句。

它通常与固定值一起使用,例如

switch (value) {
    case 1:
        // do something for value == 1
        break;
    case 2:
        // do something for value == 1
        break;
    //...
}

此外,某些值(90、80 和 70)无法转换,因为使用不一致的

<
<=
进行了奇怪的边界检查。 (如果需要包含小数十位,请将下面代码中的
>
更改为
>=

在您的情况下,

if
/
else
的序列似乎更合适:

function gradeConverting(grade) {
    let gradePercent = grade + "%";
    let americanGrade
    // note: you should probably specify what happens if grade > 100
    if (grade > 90) {
        americanGrade = "A";
    } else if (grade > 80) {
        americanGrade = "B";
    } else if (grade > 70) {
        americanGrade = "C";
    } else if (grade > 60) {
        americanGrade = "D";
    } else if (grade > 50) {
        americanGrade = "E";
    } else {
        americanGrade = "F";
    }
    return console.log("You got a " + americanGrade + " " + gradePercent + "!");
}

gradeConverting(55);

请注意,您可以缩短代码,例如这样:

function toAmericanGrade(grade) {
    if (grade < 50) {
        return "F";
    } else if (grade >= 100) {
        return "A";
    }
    // `"F".charCodeAt(0) - 1 + 5` = `74`
    return String.fromCharCode(74 - Math.floor((grade)/10))
}

function gradeConverting(grade) {
    let gradePercent = `${grade}%`;
    let americanGrade = toAmericanGrade(grade)
    return console.log(`You got a ${americanGrade}${gradePercent}!`);
}

gradeConverting(55);

// test
for(let i = 45; i <= 100; i++) {
    gradeConverting(i);
}

0
投票

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

MDN 文档是解决此类 Javascript 语法问题的最佳资源之一!

但本质上,Javascript 中的 switch 语句只接受 1 个表达式,但该表达式的多个结果。

在每个具体案例中,您可以根据该结果采取行动。

switch (value) {
    case 1:
        // do something for value == 1
        break;
    case 2:
        // do something for value == 1
        break;
    //...
}


0
投票

将 switch 语句分配给变量的情况。这个想法是使用即时调用函数,带或不带参数。

const today = (function(day){
     switch(day){
       case 1: return "Monday";
       case 2: return "Tuesday";
       case 3: return "Wednesday";
       case 4: return "Thursday";
       case 5: return "Friday";
       case 6: return "Saturday";
       case 7: return "Sunday";
       // to prevent undefined from return, add default case.
       default: return "Monday";
     }})(2);
© www.soinside.com 2019 - 2024. All rights reserved.