返回后错误丢失}和无法访问中断

时间:2013-12-18 20:52:42

标签: javascript switch-statement

所以运行时下面的代码给我两次相同的输出。我只需要它给我一个输出。如果你们能帮助我解决这个问题,那将会非常有帮助。这可能是一个愚蠢的问题,因为我是java脚本的新手。

var getReview = function (movie) {
    var review;
     switch(movie.toUpperCase()){
        case 'Matrix'.toUpperCase():
            review = console.log("Good trip out");
        break;
        case 'Princess Bride'.toUpperCase():
            review = console.log("awesome date night movie");
        break;
        case 'Welcome to America'.toUpperCase():
            review = console.log("Amjad's favorite");
        break;
        case 'Remember the Titans'.toUpperCase():
            review = console.log("love the sports");
        break;
        case "Why do I look like i'm 12?".toUpperCase():
            review = console.log("The Ryan and Zach story");
        break;
        case 'Fighting Kangaroos in the wild'.toUpperCase():
            review = console.log("Token Australian movie for Leng");
        break;
        default:
            review = console.log("I don't Know");
            break;
    }
    return review;
};
getReview("matrix");

输出:

Good trip out
good trip out

如果代码是

var getReview = function (movie) {
    var review;
     switch(movie.toUpperCase()){
        case 'Matrix'.toUpperCase():
            return("Good trip out");
        break;
        case 'Princess Bride'.toUpperCase():
            return("awesome date night movie");
        break;
        case 'Welcome to America'.toUpperCase():
            return("Amjad's favorite");
        break;
        case 'Remember the Titans'.toUpperCase():
            return("love the sports");
        break;
        case "Why do I look like i'm 12?".toUpperCase():
            return("The Ryan and Zach story");
        break;
        case 'Fighting Kangaroos in the wild'.toUpperCase():
            return("Token Australian movie for Leng");
        break;
        default:
            return("I don't Know");
            break;
    }


getReview("matrix");

输出是 错误丢失} 返回后无法恢复 两者都需要工作,只返回一个输出而不是两个。

2 个答案:

答案 0 :(得分:3)

你错过了};结束这个功能。返回后无法访问的中断与下面的代码无关紧要。返回后不要介意无法到达的休息时间。

var getReview = function (movie) {
        var review;
         switch(movie.toUpperCase()){
            case 'Matrix'.toUpperCase():
                return("Good trip out");
            break;
            case 'Princess Bride'.toUpperCase():
                return("awesome date night movie");
            break;
            case 'Welcome to America'.toUpperCase():
                return("Amjad's favorite");
            break;
            case 'Remember the Titans'.toUpperCase():
                return("love the sports");
            break;
            case "Why do I look like i'm 12?".toUpperCase():
                return("The Ryan and Zach story");
            break;
            case 'Fighting Kangaroos in the wild'.toUpperCase():
                return("Token Australian movie for Leng");
            break;
            default:
                return("I don't Know");
                break;
        }

    };
    getReview("matrix");

输出:

"Good trip out"

答案 1 :(得分:0)

你获得双倍输出的原因是因为这种形式:

review = console.log("Good trip out");

console.log调用正在将字符串打印到控制台。然后,您将console.log的结果分配给review,它似乎是它打印的字符串。

然后,当您在控制台上运行getReview('Matrix')时,控制台将自动打印返回值 - 这是打印的字符串。

因此,两个输出。