var movies = [
{
title: "Silence of the Lambs",
rating: "5 stars",
hasWatched: true
},
{
title: "Frozen",
rating: "4 stars",
hasWatched: false
},
{
title: "Inception",
rating: "1 stars",
hasWatched: true
}
]
for (i=0; i<movies.length; i++){
var result = "You have "
if (movies[i].hasWatched=true) {
result += "seen ";
} else {
result += "not seen ";
}
console.log(result + movies[i].title + " - " + movies[i].rating)
}
我试图打印&#34;你已经看过这部电影等了。&#34;如果hasWatched = true和&#34;你还没有看过这部电影等等。&#34;如果hasWatched = false。问题是,在这个版本中它总是打印&#34;看到&#34;,即使对于&#34; Frozen&#34;。我试着把电影[i] .hasWatched = false&#34;在if语句中,在这种情况下,它总是打印出来没见过。可能是什么问题?
编辑:谢谢大家,这是一个非常愚蠢的错误。大家好。
答案 0 :(得分:-1)
请更新您的代码
var result = "You have ";
for (i=0; i<movies.length; i++){
if (movies[i].hasWatched == true) { // OR if (movies[i].hasWatched) {
result += "seen ";
} else {
result += "not seen ";
}
console.log(result + movies[i].title + " - " + movies[i].rating)
}
答案 1 :(得分:-1)
试试这个
for (i=0; i<movies.length; i++){
var result = "You have ";
if (movies[i].hasWatched==true) {
result += "seen ";
} else {
result += "not seen ";
}
console.log(result + movies[i].title + " - " + movies[i].rating)
}
在条件
中使用==