[英]Javascript Objects, What am I doing wrong?
I have the following code from a coding exercise. 我从编码练习中得到以下代码。 When I try to submit it I get a
当我尝试提交它时,我得到一个
SyntaxError: Unexpected string
语法错误:意外的字符串
var movieObj = {
"Toy Story 2": "Great story. Mean prospector.",
"Finding Nemo": "Cool animation, and funny turtles."
"The Lion King": "Great songs."
};
var getReview = function (movie) {
if (movie in movieObj) {
return movieObj[movie]
} else {
return "I don't know!"
}
};
getReview("Toy Story 2") //expected = "Great story. Mean prospector."
getReview("Toy Story") //expected = " don't know!"
What am I doing wrong? 我究竟做错了什么?
You're actually just missing a comma for the second item of the movieObj object. 实际上,您只是缺少了movieObj对象第二个项目的逗号。 replace the second line with
"Finding Nemo": "Cool animation, and funny turtles.", /* Notice the comma */
It should work fine after that. 将第二行替换为
"Finding Nemo": "Cool animation, and funny turtles.", /* Notice the comma */
之后应该可以正常工作。
你忘了一个逗号后“......海龟”对象属性与分离,
You were missing a comma in your movieObj
for "Finding Nemo"
In addition, you were also missing some semi-colons ;
您在
movieObj
中缺少"Finding Nemo"
的逗号。此外,还缺少了一些分号;
var movieObj = {
"Toy Story 2": "Great story. Mean prospector.",
"Finding Nemo": "Cool animation, and funny turtles.",
"The Lion King": "Great songs."
};
var getReview = function (movie) {
if (movie in movieObj) {
alert(movieObj[movie]);
} else {
alert("I don't know!");
}
};
getReview("Toy Story 2"); //expected = "Great story. Mean prospector."
getReview("Toy Story"); //expected = " don't know!"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.