Confused about the code, help?

Following Colt Steele’se bootcamp i finnished lession about Movie Database using array’s and objects. I need a help with why did he use

result += movie.rating  + " stars";

after the if statement? Now my biggest confusion part is why did he have to use ```result += movie.rating + " stars"; when result is a "You have " string?

Here’s a code:

var movies = [
{ title: "The Burges",
  rating: 5,
  hasWatched: true
{ title: "Frozen",
  rating: 4.5,
  hasWatched: false

movies.forEach(function (movie) {
    var result = "You have ";
    if (movies.hasWatched) {
    result += "watched";
} else { 
    result += "not watched";

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

So he’s looping through the movie object. Result is “you have”. When the loop gets to the hasWatched property depending if it’s true or false he adds “watched” or “ not watched”. You can add strings together be using +.

For example

var a = “hey”;
var b = “you”;
var calling = a + b

Will give you “hey you”

So when he uses result += “watched” he’s saying result = “you have” + “watched”. Or not watched depending is haveWatched is true or false.

So now result is either you have watched, or you have not watched. Then he adds the title plus the string stars

1 Like