From 54c47b8d315f71bbc3b8f382974128fcef6a79f9 Mon Sep 17 00:00:00 2001 From: Ivan Milovanovic Date: Sun, 20 Nov 2022 22:57:29 +0100 Subject: [PATCH] Update festival.js --- festival.js | 150 ++++++++++++++++++---------------------------------- 1 file changed, 52 insertions(+), 98 deletions(-) diff --git a/festival.js b/festival.js index 2aa46ec..176cb45 100644 --- a/festival.js +++ b/festival.js @@ -8,149 +8,105 @@ function Genre(ime) { this.name = ime; - } -function Movie(title, ime, length) { +Genre.prototype.getData = function () { + const firstLetter = this.name.charAt(0); + const lastLetter = this.name.charAt(this.name.length - 1); + + return (firstLetter + lastLetter).toUpperCase(); +} - Genre.call(this, ime); +function Movie(title, genre, length) { + this.genre = genre; this.title = title; this.length = length; - } -Movie.prototype = Object.create(Genre.prototype); -Movie.prototype.constructor = Movie; - +Movie.prototype.getData = function () { + return `${this.title}, ${this.length}, ${this.genre.getData()}`; +} +/** + * + * @param {string} date Date in following format `year-month-day` for example 2022-11-20 + * @param {Array.} listOfMovies List of movies + * @param {number} totalMovies Total number of movies + */ function Program(date, listOfMovies = [], totalMovies = 0) { - this.date = new Date(); + + this.date = new Date(date); // this.listOfMovies = listOfMovies || []; 2. nacin this.listOfMovies = listOfMovies; this.totalMovies = totalMovies; } - - -function Festival(name, listOfPrograms = [], numberOfMoviesInAllPrograms = 0) { - this.name = name; - this.listOfPrograms = listOfPrograms; - this.numberOfMoviesInAllPrograms = numberOfMoviesInAllPrograms; -} - -Genre.prototype.getData = function () { - // let firstLetter = this.name[0]; - // let lastLetter = this.name[this.name.length-1]; - // let result = firstLetter + lastLetter; - // return result; - - // return (this.name[0] + this.name[this.name.length-1]).toUpperCase(); - - return (this.name.charAt(0) + this.name.charAt(this.name.length - 1)).toUpperCase(); -} - - - -Movie.prototype.getData = function () { - - // pri dodavanju jedne metode u drugu moramo koristiti: ime konstruktroske funkcije + prototip + ime funkcije + call + this u zagradi - return `${this.title}, ${this.length}, ` + Genre.prototype.getData.call(this); - -} - - -Program.prototype.addMovie = function (Movie) { +Program.prototype.addMovie = function (movie) { // za dodavanje u key, mora ispred THIS - return this.listOfMovies.push(Movie); + this.totalMovies = this.listOfMovies.push(movie); } - - -Festival.prototype.addProgram = function (Program) { - return this.listOfPrograms.push(Program) -} - - Program.prototype.getData = function () { - let sumLength = 0; - - for (let i = 0; i < this.listOfMovies.length; i++) { - //pravimo varijablu koja sadrzi sve mouvije i njihove metode, zatim koristimo key length iz liste objekata. - let movie = this.listOfMovies[i]; - sumLength = sumLength + movie.length; + const sumLength = this.listOfMovies.reduce( + (accumulator, movie) => accumulator + movie.length, + 0); - } + const printMovies = this.listOfMovies.reduce( + (accumulator, movie) => accumulator + "\t" + movie.getData() + "\n", + ""); - let printMovies = ""; + return `${this.date.getDate()}.${this.date.getMonth() + 1}.${this.date.getFullYear()}, ` + + `program length from all the movies is ${sumLength}min \n${printMovies}` +} - for (let i = 0; i < this.listOfMovies.length; i++) { - //pravimo varijablu koja sadrzi sve mouvije i njihove metode, zatim koristimo metodu koju smo pomocu prototipa vec vezali za nasu konstr funkciju Movie. - let movie = this.listOfMovies[i]; - printMovies += "\t" + movie.getData() + "\n"; - } +function Festival(name, listOfPrograms = [], numberOfMoviesInAllPrograms = 0) { + this.name = name; + this.listOfPrograms = listOfPrograms; + this.numberOfMoviesInAllPrograms = numberOfMoviesInAllPrograms; +} - return `${this.date.getDate()}.${this.date.getMonth() + 1}.${this.date.getFullYear()}, program length from all the movies is ${sumLength}min \n${printMovies}` +Festival.prototype.addProgram = function (program) { + this.listOfPrograms.push(program); + this.numberOfMoviesInAllPrograms += program.totalMovies; } Festival.prototype.getData = function () { - // return name + " festival has " numMovies + "movie titles" - // "output" for() - + const sumMovies = this.listOfPrograms.reduce( + (accumulator, program) => accumulator + program.totalMovies, + 0); // must set initial value to get primitive value result, returns object otherwise - let sumMovies = 0; + const sumPrograms = this.listOfPrograms.reduce( + (accumulator, program) => accumulator + "\t" + program.getData() + "\n", + ""); - for (let i = 0; i < this.listOfPrograms.length; i++) { - let program = this.listOfPrograms[i]; - sumMovies += program.listOfMovies.length; - } - - - let sumPrograms = ""; - for (let i = 0; i < this.listOfPrograms.length; i++) { - let naziv = this.listOfPrograms[i]; - sumPrograms += "\t" + naziv.getData() + "\n"; - } - - - return `${this.name} festival has ${sumMovies} movie titles \n${sumPrograms}` + return `${this.name} festival has ${sumMovies} movie titles \n${sumPrograms}`; } - function createMovie(title, genre, length) { - - return new Movie(title, genre, length) - + return new Movie(title, new Genre(genre), length) } function createProgram(date) { return new Program(date); } +const noviFilm = createMovie("titanik", "romance", 200); +const mara = createMovie("Marija", "action", 190); +const nemanja = createMovie("Nemanja", "romantic", 90); +const marko = createMovie("Marko", "drama", 50); - -var noviFilm = createMovie("titanik", "romance", 200); -console.log(noviFilm); - -const mara = new Movie("Marija", "action", 190); -const nemanja = new Movie("Nemanja", "romantic", 90); -console.log(mara.getData()); - -const marko = new Movie("Marko", "drama", 50) - -const exit = new Program("15.07.2022", []); -const fest = new Program("16.07.2022", []); - +const exit = createProgram("2022-07-15"); +const fest = createProgram("2022-07-16"); const leto = new Festival("leto"); - exit.addMovie(mara); exit.addMovie(nemanja); fest.addMovie(marko); @@ -159,5 +115,3 @@ leto.addProgram(exit); leto.addProgram(fest); console.log(leto.getData()); - -// console.log(exit.getData());