我在编码练习中有以下代码。当我尝试提交时,我得到了
SyntaxError:意外的字符串
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!"
我做错了什么?
答案 0 :(得分:6)
你实际上只是错过了movieObj对象的第二项的逗号。用。替换第二行
"Finding Nemo": "Cool animation, and funny turtles.",
/* Notice the comma */
之后应该可以正常工作。
答案 1 :(得分:0)
在“... turtles”对象属性与,
答案 2 :(得分:0)
您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!"
function forEach(array, action) {
for (var i = 0; i< array.length; i++)
action(array[i]); // array[i] is the argument
}