-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (73 loc) · 2.41 KB
/
Copy pathscript.js
File metadata and controls
99 lines (73 loc) · 2.41 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
let allMovies = [];
//Define a movie class with parameters title (string), rating (number) and haveWatched (boolean)
class Movie {
constructor(title, rating, haveWatched) {
this.title = title;
this.rating = rating;
this.haveWatched = haveWatched;
}
}
//add a movie OBJECT to the allMovies array
let addMovie = (movie) => {
allMovies.push(movie);
console.log("A new movie is added");
}
//iterate through all elements of allMovies array
//print out to console in a correct format
//print out the total number of movies in allMovies array
let printMovies = () => {
console.log("Printing all movies...");
for (let i = 0; i < allMovies.length; i++) {
console.log(allMovies[i].title + ", rating of " + allMovies[i].rating + ", havewatched: " + allMovies[i].haveWatched);
}
console.log("You have " + allMovies.length + " movies in total");
}
//print out to console, only the movies that has a rating higher than rating(argument)
//print out the total number of matches
let highRatings = (rating) => {
console.log("printing movie that has a rating higher than " + rating);
var matches = 0;
for (let i = 0; i < allMovies.length; i++) {
if (allMovies[i].rating > rating) {
console.log(allMovies[i].title + " has a rating of " + allMovies[i].rating);
matches++;
}
}
console.log("In total, there are " + matches + " matches");
}
//Toggle the 'haveWatched' property of the specified movie
let changeWatched = (title) => {
console.log("changing the status of the movie...");
for (let i = 0; i < allMovies.length; i++) {
if (allMovies[i].title == title) {
if (allMovies[i].haveWatched) {
allMovies[i].haveWatched = false;
}else {
allMovies[i].haveWatched = true;
}
}
}
}
////////////////////////////////////////////////////////////
//Test code - DO NOT DELETE OR EDIT
let x = new Movie("Spiderman", 3, true);
let y = new Movie("Citizen Kane", 4, false);
let z = new Movie("Zootopia", 4.5, true);
allMovies.push(x,y,z);
console.log("----------------");
console.log("running program......");
console.log("----------------");
printMovies();
let movie1 = new Movie("Parasite", 2, false);
console.log("----------------");
addMovie(movie1);
console.log("----------------");
changeWatched("Spiderman");
console.log("----------------");
printMovies();
console.log("----------------");
changeWatched("Spiderman");
console.log("----------------");
printMovies();
console.log("----------------");
highRatings(3.5);