当前位置: 代码迷 >> JavaScript >> Javascript对象,我在做什么错?
  详细解决方案

Javascript对象,我在做什么错?

热度:96   发布时间:2023-06-13 12:46:16.0

我从编码练习中得到以下代码。 当我尝试提交它时,我得到一个

语法错误:意外的字符串

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

我究竟做错了什么?

实际上,您只是缺少了movieObj对象第二个项目的逗号。 将第二行替换为"Finding Nemo": "Cool animation, and funny turtles.", /* Notice the comma */之后应该可以正常工作。

你忘了一个逗号后“......海龟”对象属性与分离,

您在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!"
  相关解决方案