嵌套在函数中的JavaScript switch语句

时间:2013-06-22 23:45:41

标签: javascript

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

当我提交上面的代码时,我收到错误消息:SyntaxError - missing:在case标签之后。语法有什么问题?

4 个答案:

答案 0 :(得分:4)

更改

case 'Why do I look like I'm 12?':

case 'Why do I look like I\'m 12?':

答案 1 :(得分:2)

引用'Why do I look like I'm 12?':'Why do I look like I\'m 12?':。此外,返回后您不需要break

答案 2 :(得分:2)

case 'Why do I look like I'm 12?':

将其改为:

case 'Why do I look like I\'m 12?':

你有引用问题

答案 3 :(得分:1)

JS认为你正在关闭Why do I look like I'的字符串。为避免这种情况,您必须使用\

来转义撇号
case 'Why do I look like I\'m 12?':