0
 // Create movie DB
var movies = [
    {
        title: "Avengers",
        hasWatched: true,
        rating: 5
     },
    {  
        title: "SpiderMan",
        hasWatched: false,
        rating: 4

      },
    {
        title: "LightsOut",
        hasWatched: true,
        rating: 6
     }
]

// Print it out
movies

// Print out all of them 
movies.forEach(function(movie)){
  var result = "You have ";
  if(movie.hasWatched){
       result += "watched ";
}else{
       result += "not seen ";
}

  result += "\" + movie.title + "\"-";
  result += movie.rating + " stars";
  console.log(result);
}

Why my first Object is true but the console print out " You have not seen "Avengers" - 5 stars "

str
  • 42,689
  • 17
  • 109
  • 127
Simon
  • 15
  • 1

2 Answers2

0

Here is the updated code. Fixed few syntax errors!

var movies = [{
  title: "Avengers",
  hasWatched: true,
  rating: 5
}, {
  title: "SpiderMan",
  hasWatched: false,
  rating: 4

}, {
  title: "LightsOut",
  hasWatched: true,
  rating: 6
}]

// Print it out
console.log(movies);

// Print out all of them
movies.forEach(function(movie) {
  var result = "You have ";
  if (movie.hasWatched) {
    result += "watched ";
  } else {
    result += "not seen ";
  }

  result += '"' + movie.title + '" - ';
  result += movie.rating + " stars";
  console.log(result);
});
Pugazh
  • 9,453
  • 5
  • 33
  • 54
0

You had a syntax error at this line:

result += "\" + movie.title + "\"-";

It should be fixed with:

result += '"' + movie.title + '"- ';

In JavaScript both double and single quotes can be used, in your case you want to print out double quotes ("), you can "wrap" them in with a single quote ex: '"'.

Interesting discussion on when to use double/single quotes can be found here.

var movies = [{
  title: "Avengers",
  hasWatched: true,
  rating: 5
}, {
  title: "SpiderMan",
  hasWatched: false,
  rating: 4

}, {
  title: "LightsOut",
  hasWatched: true,
  rating: 6
}]

// Print it out
console.log(movies);

// Print out all of them 
movies.forEach(function(movie) {
  var result = "You have ";
  if (movie.hasWatched) {
    result += "watched ";
  } else {
    result += "not seen ";
  }
  result += '"' + movie.title + '"- ';
  result += movie.rating + " stars";
  console.log(result);
});
Community
  • 1
  • 1
GibboK
  • 71,848
  • 143
  • 435
  • 658