inkplay_
inkplay_

Reputation: 307

Javascript object returns undefined

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?

Upvotes: 1

Views: 103

Answers (6)

Nina Scholz
Nina Scholz

Reputation: 386522

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>');
});

Upvotes: 0

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22875

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);

});

Upvotes: 0

Rodmentou
Rodmentou

Reputation: 1640

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.

Upvotes: 0

R. Oosterholt
R. Oosterholt

Reputation: 8080

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);
}

Upvotes: 1

Benny Schmidt
Benny Schmidt

Reputation: 3394

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

Upvotes: 1

Ramanlfc
Ramanlfc

Reputation: 8354

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

Upvotes: 3

Related Questions