1
var movies = 
[
{name: "In Bruges", rating:"4.7", seen:false},
{name: "Frozen", rating: "4.5", seen:true},
{name: "Lion King", rating:"5", seen:true},
]


for (var i = 0; i < movies.length; i++) {
    var result = "You have ";
    if(movies.seen === true){
        result += "watched ";
    }
    else{
        result += "not seen ";
    }
    result += "\"" + movies.name + "\" - ";
    result += movies.rating + " stars"
    console.log(result)
};

You have not seen "undefined" - undefined stars is the result in chrome however you should see You have seen/not seen "movie name" - "rating" stars.

I need to use for loop to print out what each movie I have watched and rating and if I have seen it or not. Question is why is it undefined? Should the code see movies.rating and just substitute the value there? Can some one check my code and help me with my for loop?

2
  • Refer array elements with index, if(movies.seen === true) to if(movies[i].seen === true). or use forEach loop. Commented Nov 24, 2015 at 19:59
  • 1
    @rajuGT Awesome it worked, adding i to both movies.seen/name/rating made it work. Thanks! Commented Nov 24, 2015 at 20:03

6 Answers 6

3

movies is an array movies.seen won't work use movies[i].seen and like wise for other properties

Sign up to request clarification or add additional context in comments.

Comments

1

Change movies.seen, movies.rating and movies.name to movies[i].seen, movies[i].rating, and movies[i].name.

Comments

1

Use index to access item of array; e.g. movies[i]:

for (var i = 0; i < movies.length; i++) {
    var result = "You have ";
    if(movies[i].seen === true){
        result += "watched ";
    }
    else{
        result += "not seen ";
    }
    result += "\"" + movies[i].name + "\" - ";
    result += movies[i].rating + " stars"
    console.log(result)
}

Or you could store the array item in a variable and use it like this:

for (var i = 0; i < movies.length; i++) {
    var movie = movies[i];
    var result = "You have ";
    if (movie.seen) {
        result += "watched ";
    }
    else {
        result += "not seen ";
    }
    result += '"' + movie.name + '" - ';
    result += movie.rating + " stars";
    console.log(result);
}

Comments

0

Here is a working Plunker for you to see it working. :)

var movies = 
[
{name: "In Bruges", rating:"4.7", seen:false},
{name: "Frozen", rating: "4.5", seen:true},
{name: "Lion King", rating:"5", seen:true},
]


for (var i = 0; i < movies.length; i++) {
    var result = "You have ";
    if(movies[i].seen === true){
        result += "watched ";
    }
    else{
        result += "not seen ";
    }
    result += "\"" + movies[i].name + "\" - ";
    result += movies[i].rating + " stars"
    alert(result)
}

You were trying to access properties of movies called seem, name and rating, but movies has 3 objects indexed from 0 to 2.

Comments

0

You are using movies inside loop and movies do not contain those properties. And it is allways safe to use forEach loop while iterating arrays

Do something like

movies.forEach(function(movie){
var result = "You have ";
    if(movie.seen === true){
        result += "watched ";
    }
    else{
        result += "not seen ";
    }
    result +=  movie.name + " - ";
    result += movie.rating + " stars";
    console.log(result);

});

Comments

0

I suggest to use a little other approach to iterate over the array and for the check if a movie has been seen.

var movies = [
    { name: "In Bruges", rating: "4.7", seen: false },
    { name: "Frozen", rating: "4.5", seen: true },
    { name: "Lion King", rating: "5", seen: true },
];

movies.forEach(function (movie) {
    var result = "You have ";            
    result += movie.seen ? "watched " : "not seen ";            
    result += "\"" + movie.name + "\" - ";
    result += movie.rating + " stars"
    document.write(result + '<br>');
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.