-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-Movie.js
60 lines (31 loc) · 2.14 KB
/
class-Movie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// a) Write a constructor for the class Movie, which takes a String representing the title of the movie, a String representing the studio, and a String representing the rating as its arguments, and sets the respective class properties to these values.
// b) The constructor for the class Movie will set the class property rating to "PG" as default when no rating is provided.
//c) Write a method getPG, which takes an array of base type Movie as its argument, and returns a new array of only those movies in the input array with a rating of "PG". You may assume the input array is full of Movie instances. The returned array need not be full.
// d) Write a piece of code that creates an instance of the class Movie with the title “Casino Royale”, the studio “Eon Productions”, and the rating “PG13”
class Movie {
constructor(title, studio, rating = "PG") { // (constructor - a) & (default rating declaration - b)
this.title = title;
this.studio = studio;
this.rating = rating;
}
static getPG(input) { // (c)
let ratingPG = input.filter(movie => movie.rating === "PG");
return ratingPG;
}
}
const movie1 = new Movie ("Casino Royale", "Eon Productions", "PG13"); // (d)
const movie2 = new Movie ("Ghilli", "Sri Surya Movies"); // (example for dafault rating when no rating provided - b)
const movie3 = new Movie ("Attahasam", "Vijayam Cine");
const movie4 = new Movie ("Virumaandi", "Raj Kamal Films");
let movieArray = [movie1, movie2, movie3, movie4];
let pgMovies = Movie.getPG(movieArray);
console.log(movie1); // (output for - d)
// (output for - d) => Movie { title: 'Casino Royale', studio: 'Eon Productions', rating: 'PG13'}
console.log(movie2); // (output for - b)
// (output for - b) => Movie { title: 'Ghilli', studio: 'Sri Surya Movies', rating: 'PG' }
console.log(pgMovies); // (output for - c)
/* (output for - c) => [
Movie { title: 'Ghilli', studio: 'Sri Surya Movies', rating: 'PG' },
Movie { title: 'Attahasam', studio: 'Vijayam Cine', rating: 'PG' },
Movie { title: 'Virumaandi', studio: 'Raj Kamal Films', rating: 'PG' }
] */