一个switch循环 说是SyntaxError: Unexpected token {

nineteen73 2013-04-30 01:12:58
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 {
不知道哪里错了,各位给看看吧
...全文
945 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
nineteen73 2013-05-01
  • 打赏
  • 举报
回复
[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! 通过了,谢谢你哈
KK3K2005 2013-05-01
  • 打赏
  • 举报
回复
if(movie in movies){ return movies[movie]; } return "I don't know!" -----》 return movies[movie] || "I don't know!"; 另一个方法更简洁的原因 是 switch 可以抽象成一个map映射(配置重构)
ftiger 2013-04-30
  • 打赏
  • 举报
回复
我的代码通过了,要返回值,而不是打印到控制台

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
nineteen73 2013-04-30
  • 打赏
  • 举报
回复
引用 10 楼 ftiger 的回复:
你这个是什么运行环境?
codecademy http://www.codecademy.com/zh/courses/spencer-sandbox/0/4?curriculum_id=506324b3a7dffd00020bf661#
ftiger 2013-04-30
  • 打赏
  • 举报
回复
你这个是什么运行环境?
nineteen73 2013-04-30
  • 打赏
  • 举报
回复
引用 5 楼 zsx841021 的回复:
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");

完全复制了,然后还是
nineteen73 2013-04-30
  • 打赏
  • 举报
回复
输入:
输出:
报错:
ftiger 2013-04-30
  • 打赏
  • 举报
回复
case'Why do I look like I\'m 12?': //注意要转义
三石-gary 2013-04-30
  • 打赏
  • 举报
回复
双引号里面用单引号。。如果单引号里面用单引号需要转义
三石-gary 2013-04-30
  • 打赏
  • 举报
回复
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");
三石-gary 2013-04-30
  • 打赏
  • 举报
回复
报什么错。。。
nineteen73 2013-04-30
  • 打赏
  • 举报
回复
这是改过之后的,但还是有错 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");
三石-gary 2013-04-30
  • 打赏
  • 举报
回复
CASE 后面该加break的时候还是应该加上
ftiger 2013-04-30
  • 打赏
  • 举报
回复

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!";    
    }
 };

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧