Now it's time for you to write some of your own code using javascript objects.
We're just going to do one quick exercise.
You're going to build a movie database array.
So that sounds much more complicated than it is.
The idea is that you have an array called movies and inside of that array you have a bunch of movie
objects.
So it's just like the posting comments thing that we saw earlier.
So a movie should have a title that's a string a rating that's a number and has watched which is a boolean
.
So that's just supposed to determine if you have seen the movie or if you haven't seen the movie.
So I want to define an array with three or four different movies in it.
Then you're going to loop through the array and print out something that looks like this.
So on each line you will either say you have watched the movie title dash the number of stars the rating
or if has watched is false.
You're right you have not seen the movie title dash the rating.
So again you're going to have an array of movie objects.
Each one has a title rating and has watched which is the.
Then you're going to loop through that array of movies and print out a line for each one.
And that line will include.
If you have or haven't seen the movie the title and the rating.
OK so possible you attempt this on your own and then I'll be back in a few seconds to go over the answer
.
OK.
So let's start writing the solution.
I'm going to open up the console and declare a movie's array.
So far movies equals an array.
And then inside the Saray we're going to put a bunch of objects.
So each movie object is going to have a title
and I'm just going to do two.
So we don't spend a lot of time here title in Bruges.
Great movie if you haven't seen it.
Has watched.
That will be true for me and rating is five.
And because that's just getting a little bit long I'm going to indent this put every property on its
own line.
It's just much more readable.
So we'll do one more movie and that will be tidal frozen which I actually haven't seen.
So that's false and rating will be four point five.
That's just going based off of what I've heard and we'll leave our movies at that.
So two movies each one has a title.
A string has watched a boolean and rating a number.
So let's hit enter and take a look at our movies array objects.
Just like we expect.
So now all we have to do is loop through the array and then print out this nice string for every movie
.
So I'm going to use a for each.
So movies for each.
But of course you could also easily use a for loop or whatever you prefer.
So for each one we'll give it a placeholder name of movie and that will open this up and all that we
want to do is print out a line for each movie.
So that's going to be a long comp's thought log.
And rather than do a giant Cazotte log that has a bunch of plus signs in it.
I'm actually going to make a string called result.
So that's going to look like this var results equals and it's going to start as you have because if
you look over here they all start with you have you have.
So I'm going to start my string off like that and then I'm going to have an if statement to decide what
comes next I either I'm going to add watched or I'm going to add not seen.
So to do that.
Make sure you have your space right here and then on the next line just have an if statement.
If movie has watched.
So if we have watched it we're going to set result two plus equal watched with the space after it or
else we're going to set result plus equals not scene.
Also with the space after it.
So we're basically building this string that we're going to print at the end.
So it starts off.
You have space and then depending on the value of has watched for each movie we're either going to add
in or concatenate watched or not seen then the next step is to go ahead and add in the movie the title
.
So next we do a result for us equals and you will see there are quotes around the title.
So if we wanted to make those quotes work we need to make sure that we have the backslash quote to escape
the double quotes.
So we're going to result plus equals and then we're going to start off with our quote.
So this is a little bit weird looking.
And then we're going to add plus movie dot title plus.
And then the closing quote at the end which we have to just do backslash quote.
So it's a lot of quotation marks.
Remember that this turns into just a double quote and this turns into a double quote.
And then let's add a space after that double quote and a dash and then the very last thing is to add
in result plus equals the number of stars.
So that's going to be you don't need quote movie rating plus
stars just like that.
And then we're going to cost that log result and we end up with this here.
You have watched in Bruges five stars you have not seen Frozen 4.5 stars.
And we can go ahead and add one more movie in here
to title the miserables
rating 3.5 and has watched set that to false.
I've seen the musical never the movie and let's indent this just so that it's easier to read and read
you this.
So hit enter here and then run our code again to print things out and we can walk through this one more
time.
We make a result string each time in the for loop and we're going to slowly build that result string
.
So let's take the example of in Bruges.
We're going to add you have space.
And then if movie has watched is true which it is we're going to plus equals watched.
So we end up with you have watched and then the space after that and then replace the coil in a double
quote.
Plus the movie title plus another movie quote.
So then we have.
You have watched double quote In Bruges and double quote dash space and then we add in the movie rating
and stars.
So that movie rating is five.
So we end up with you have watched in Bruges dash five stars and we print that out.
You can see it works just fine.
And our newly added Les Miserables shows up as you have not seen.
Les Miserables 3.5 stars.
And that's all we needed to do.
If you wanted to refactor this a little bit it might be a good idea is to move most of the logic in
here.
Aside from the printing move everything out into a separate function.
So I'm going to copy this and make a function called Build string and that will take a single movie
object and then inside if they're going to add all this code which takes a result variable and it slowly
builds it up exactly the same.
I kept it named movie so we don't have to change anything.
And then the very last thing would be return result and that's all we need there and then we need to
change our for loop a little bit.
So we're going to do movie stuff for each function each movie
and for each one what we're going to do is go ahead and run build string of movie and to print it will
just run cancel that log filled string movie.
So this cleaned it up a lot.
Just remember when rerun build string we pass in each movie.
This makes a string and it returns the result.
So this right here is going to be replaced with whatever string is returned and that will be printed
out.
So we end up with this.
So it's a little cleaner.
We end up with this nice simple for each.
That's very clear what's happening.
Print Howsabout log.
Build string for each movie.
It makes a lot of sense if you just look at it.
You can immediately tell what it's supposed to do.
Based off of the name.
OK so that's it.