Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window.onload = alert("Hello World");
- //displayShowTime(); function() {
- // Version 1
- function displayShowTime(){
- var display1 = getNextShowing(movie1);
- alert(display1);
- var display2 = getNextShowing(movie2);
- alert(display2);
- }
- function Movie(title, genre, rating, showtimes) {
- this.title = title;
- this.genre = genre;
- this.rating = rating;
- this.showtimes = showtimes;
- this.getNextShowing = function() {
- var now = new Date().getTime();
- for (var i = 0; i < this.showtimes.length; i++) {
- var showtime = getTimeFromString(this.showtimes[i]);
- if ((showtime - now) > 0) {
- return "Next showing of " + this.title + " is " + this.showtimes[i];
- }
- }
- return null;
- };
- }
- function getNextShowing(movie) {
- var now = new Date().getTime(); //get the time on your computer
- for (var i = 0; i < movie.showtimes.length; i++) { //iterate for the showime
- var showtime = getTimeFromString(movie.showtimes[i]); // transform a string into real time and assign to a var
- if ((showtime - now) > 0) { // if 09:00PM - 08:00PM = 1 > 0 this says that the time did not arrived so the movie is on the next schedule
- return "Next showing of " + movie.title + " is " + movie.showtimes[i]; //return a string with the name and showtime of the movie
- } else {
- return "There´s no more showtime for" + movie.title " today. Sorry";
- }
- }
- }
- var movie1 = {
- title: "Plan 9 from Outer Space",
- genre: "Cult Classic",
- rating: 2,
- showtimes: ["3:00pm", "7:00pm", "11:00pm"],
- };
- var movie2 = {
- title: "Forbidden Planet",
- genre: "Classic Sci-fi",
- rating: 5,
- showtimes: ["5:00pm", "9:00pm"],
- };
- function getTimeFromString(str) {
- var theTime = new Date(); //create a date object and assign to the var theTime
- var time = str.match(/(\d+)(?::(\d\d))?\s*(p?)/); //understand the string.match function and its parameters
- theTime.setHours( parseInt(time[1]) + (time[3] ? 12 : 0) ); //
- theTime.setMinutes( parseInt(time[2]) || 0 );
- return theTime.getTime(); //understand .getTime
- }
Add Comment
Please, Sign In to add comment