-1

When I run the very simple following code, for some reason I get the following result in the browser console: "6 You have not watched undefined undefined." Can anyone point out my error please?

var movies = [{
 title: "The Mummy",
 hasWatched: true,
 stars: "5 stars"
 },
 {
 title: "About A Boy",
 hasWatched: true,
 stars: "5 stars"
 },
 {
 title: "It",
 hasWatched: false,
 stars: "5 stars"
 },
 {
 title: "Cleopatra",
 hasWatched: false,
 stars: "5 stars"
 }
];
for (var i = 0; i <= movies.length; i++) {
 if (movies.hasWatched) {
 console.log("You have watched " + movies.title + " " + movies.stars + ".");
 } else {
 console.log("You have not watched " + movies.title + " " + movies.stars + ".");
 }
}

j08691
209k33 gold badges269 silver badges281 bronze badges
asked Sep 26, 2017 at 2:34
1
  • movies is an array. You need to index it with i. Ie movies[i].hasWatched Commented Sep 26, 2017 at 2:36

3 Answers 3

3

You have an array of objects so you need to reference the index of each array element. You also need to reduce your loop by one since array indexes are zero-based but the length is not.

var movies = [{
 title: "The Mummy",
 hasWatched: true,
 stars: "5 stars"
 },
 {
 title: "About A Boy",
 hasWatched: true,
 stars: "5 stars"
 },
 {
 title: "It",
 hasWatched: false,
 stars: "5 stars"
 },
 {
 title: "Cleopatra",
 hasWatched: false,
 stars: "5 stars"
 }
];
for (var i = 0; i < movies.length; i++) {
 if (movies[i].hasWatched) {
 console.log("You have watched " + movies[i].title + " " + movies[i].stars + ".");
 } else {
 console.log("You have not watched " + movies[i].title + " " + movies[i].stars + ".");
 }
}

answered Sep 26, 2017 at 2:36
Sign up to request clarification or add additional context in comments.

Comments

2

Change the for condition to i < movies.length; you have one extra iteration. And you also need to references movies[i] to get the actual movie, for example, movies[i].title.

In the above example, the last index is 3 (items are numbered 0, 1, 2, 3), but your loop is going until 4 and will attempt to find movies[4].title and return undefined.

answered Sep 26, 2017 at 2:36

Comments

1
for (var i = 0; i <= movies.length; i++) {
 if (movies[i].hasWatched) {
 console.log("You have watched " + movies[i].title + " " + movies[i].stars + ".");
 } else {
 console.log("You have not watched " + movies[i].title + " " + movies[i].stars + ".");
 }
}

you were just missing the index identifier while accessing

Jose CC
86612 silver badges24 bronze badges
answered Sep 26, 2017 at 2:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.