Yesterday I talked about my plan to better my knowledge of the JavaScript language. Today I continue this mission with a new challenge.
/*
- create a movie object that takes in five arguments
- arguments: title, director, genre, release year and rating
- make a function that logs an overview of the movie:
'Movie', a 'genre' film released in 'year', was directed by 'director' and recieved a score 'score'.
*/
This challenge looks similar to the book keeping challenge. I also solved it similarely, so if you want to have another example of a similar challenge you can find it here.
Create a movie class
First I created a movie object. This can be done using functions or classes. Since I work mostly with functions I decided to focus on working with classes for this challenge.
class Movie {
constructor(title, director, genre, releaseYear, rating) {
this.title = title;
this.director = director;
this.genre = genre;
this.releaseYear = releaseYear;
this.rating = rating;
}
}
I also created a few movie objects, so that I can test the function.
const firstMovie = new Movie(
"The best movie ever written",
"mr. Moviemaker",
"Comedy",
"1984",
"1.5/10"
);
const secondMovie = new Movie(
"I, the movie",
"mrs. MovieLover666",
"Horror",
"2020",
"7.5/10"
);
const thirdMovie = new Movie(
"The mountain movie",
"MovieChild",
"Thriller",
"2012",
"8.0/10"
);
Create an overview function
All I need this function to do is, return a string containing all of the elements of the movie. This string can be then console loged at the end of the app.
Movie.prototype.movieOverview = function () {
return `${this.title}, a ${this.genre} film released in ${this.releaseYear}, was directed by ${this.director} and recieved a score ${this.rating}.`;
};
I tried out the movieOverview()
function by using it on the three movies I defined at the beginning. Based on the results I got in the console it looks like the function works as intended.
console.log(firstMovie.movieOverview());
console.log(secondMovie.movieOverview());
console.log(thirdMovie.movieOverview());
/* Result:
The best movie ever written, a Comedy film released in 1984, was directed by mr. Moviemaker and recieved a score 1.5 / 10.
I, the movie, a Horror film released in 2020, was directed by mrs. MovieLover666 and recieved a score 7.5 / 10.
The mountain movie, a Thriller film released in 2012, was directed by MovieChild and recieved a score 8.0 / 10.
*/
Conclusion
This concludes my second challenge. It is quite similar to the previous one, that had to do with book keeping. I think it is good to rehearse what you already know. I also must say that writing it all up in a post like this helps me a lot with understanding and remembering what I am/was doing. I will continuoue to write about the challenges and other things as much as possible.
Top comments (0)