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
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
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!";
}
};
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");
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");
http://www.codecademy.com/zh/courses/spencer-sandbox/0/4?curriculum_id=506324b3a7dffd00020bf661#
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
return movies[movie];
}
return "I don't know!"-----》return movies[movie] || "I don't know!";另一个方法更简洁的原因 是 switch 可以抽象成一个map映射(配置重构)