我一直在寻找,但我无法得到明确的答案。(我是 javascript 的初学者,这是控制台内的测试/练习)我如何在 for 循环中创建一个本地范围,一个全局范围,然后我可以将它添加到带有对象数组的变量中。例如:我希望用户在名为“stars”的变量(提示)中设置一部电影有多少颗星(评级)。然后可以在对象/数组中的movieDb变量中调用它,其中显示“评级:”......使用变量stars,这样它就可以控制台记录用户输入的星星以及其他信息。这是允许的还是有不同的方法?
var movieDb =[
{
haswatched : "i",
title : "In Burges",
rating : stars,
},
{
haswatched : "i",
title : "Frozen",
rating : stars,
},
{
haswatched : "i",
title : "Mad Max",
rating : stars,
},
]
for (var i = 0; i < movieDb.length ; i++) {
var w = prompt("did you watch the movie?")
if (w === 'yes'){//first IF
movieDb[i].haswatched = "you have watched";
var r = prompt("would you like to rate us?");
if (r === "yes") {//nested in first IF
var stars = prompt("Enters Stars");
alert("Thanks for rating & Watching!")
} else if(r === "no"){ //nested in second IF
alert("You did not rate, Thanks for watching!")
}
}
else if (w === "no"){ //first ELSE IF
movieDb[i].haswatched = "you have not seen";
}
console.log(movieDb[i].haswatched + " " + "\""+movieDb[i].title+"\"" + " -" + movieDb[i].rating);
}'