var getReview = function (movie) {
    
    switch(movie){
        case(Matrix){
            return"good trip out";
        }
        case(Princess Bride){
            return"awesome date night movie";
        }
        case(Welcome to America){
            return"Amjad's favorite";
        }
        case(Remember the Titans){
            return"love the sports";
        }
        case(Why do I look like ) {
           return"The Ryan and Zach story";
        }   
        
        case(Fighting Kangaroos in the wild){
            return "Token Australian movie for Leng";
            
        }
        fault{
        return"I don't know!";    
        }
            
        }
    };getReview(Princess Bride);
SyntaxError: Unexpected token {
不知道哪里错了,各位给看看吧switch函数js

解决方案 »

  1.   


    var getReview = function (movie) {
        
        switch(movie){
            case "Matrix": //case语法错误 而且字符串要用引号括起来。
                return"good trip out";
            
            case "Princess Bride":
                return"awesome date night movie";
            
            case "Welcome to America":
                return"Amjad's favorite";
            
            case "Remember the Titans":
                return"love the sports";
            
            case "Why do I look like":
               return"The Ryan and Zach story";
            
            
            case "Fighting Kangaroos in the wild":
                return "Token Australian movie for Leng";
                
            //fault //这里是不是拼写错误?
            default :
            return"I don't know!";    
        }
     };
      

  2.   

    CASE 后面该加break的时候还是应该加上
      

  3.   

    这是改过之后的,但还是有错
    var getReview = function (movie) {
        
        switch(movie){
            case'Matrix':
                console.log("good trip out");
           break;
            case'Princess Bride':
                console.log("awesome date night movie");
           break;
            case'Welcome to America':
                console.log("Amjad's favorite");
           break;    
            case'Remember the Titans':
                console.log("love the sports");
            break;
            case'Why do I look like I'm 12?':
               console.log("The Ryan and Zach story");
            break;  
            case'Fighting Kangaroos in the wild':
                console.log("Token Australian movie for Leng");
            break;
            default:
            console.log("I don't know!");    
           }
        };
        .getReview("Fighting Kangaroos in the wild");
      

  4.   

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

  5.   

     case'Why do I look like I\'m 12?': //注意要转义
      

  6.   

    codecademy
    http://www.codecademy.com/zh/courses/spencer-sandbox/0/4?curriculum_id=506324b3a7dffd00020bf661#
      

  7.   

    我的代码通过了,要返回值,而不是打印到控制台var getReview = function (movie) {
         
        switch(movie){
            case "Matrix": //case语法错误 而且字符串要用引号括起来。
                return"good trip out";
             
            case "Princess Bride":
                return"awesome date night movie";
             
            case "Welcome to America":
                return"Amjad's favorite";
             
            case "Remember the Titans":
                return"love the sports";
             
            case "Why do I look like I'm 12?":
               return"The Ryan and Zach story";
             
             
            case "Fighting Kangaroos in the wild":
                return "Token Australian movie for Leng";
                 
            //fault //这里是不是拼写错误?
            default :
            return"I don't know!";    
        }
     };
        getReview("Matrix");
    Way to go! 另,另一个方法没通过,但这代码明显更精减。var getReview = function (movie) {
        var movies= {
            "Matrix":"good trip out",
            "Princess Bride" : "awesome date night movie",
            "Welcome to America" : "Amjad's favorite",
            "Remember the Titans" : "love the sports",
            "Why do I look like I'm 12?" : "The Ryan and Zach story",
            "Fighting Kangaroos in the wild" : "Token Australian movie for Leng"
        }
        
        if(movie in movies){
            return movies[movie];
        }
        return "I don't know!"
    };
        getReview("Matrix");Oops, try again! Better use a switch statement
      

  8.   

    if(movie in movies){
            return movies[movie];
        }
        return "I don't know!"-----》return movies[movie] || "I don't know!";另一个方法更简洁的原因 是 switch 可以抽象成一个map映射(配置重构)
      

  9.   

    Quote: 引用 12 楼 ftiger 的回复:

    我的代码通过了,要返回值,而不是打印到控制台var getReview = function (movie) {
         
        switch(movie){
            case "Matrix": //case语法错误 而且字符串要用引号括起来。
                return"good trip out";
             
            case "Princess Bride":
                return"awesome date night movie";
             
            case "Welcome to America":
                return"Amjad's favorite";
             
            case "Remember the Titans":
                return"love the sports";
             
            case "Why do I look like I'm 12?":
               return"The Ryan and Zach story";
             
             
            case "Fighting Kangaroos in the wild":
                return "Token Australian movie for Leng";
                 
            //fault //这里是不是拼写错误?
            default :
            return"I don't know!";    
        }
     };
        getReview("Matrix");
    Way to go! 通过了,谢谢你哈