Guest User

Untitled

a guest
Feb 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.onload = alert("Hello World");
  2. //displayShowTime(); function() {
  3.     // Version 1
  4.    
  5.  
  6. function displayShowTime(){
  7.     var display1 = getNextShowing(movie1);
  8.     alert(display1);
  9.     var display2 = getNextShowing(movie2);
  10.     alert(display2);
  11. }
  12.  
  13. function Movie(title, genre, rating, showtimes) {
  14.     this.title = title;
  15.     this.genre = genre;
  16.     this.rating = rating;
  17.     this.showtimes = showtimes;
  18.     this.getNextShowing = function() {
  19.         var now = new Date().getTime();
  20.         for (var i = 0; i < this.showtimes.length; i++) {
  21.             var showtime = getTimeFromString(this.showtimes[i]);
  22.             if ((showtime - now) > 0) {
  23.                 return "Next showing of " + this.title + " is " + this.showtimes[i];
  24.             }
  25.         }
  26.         return null;
  27.     };
  28. }
  29.  
  30. function getNextShowing(movie) {
  31.         var now = new Date().getTime(); //get the time on your computer
  32.         for (var i = 0; i < movie.showtimes.length; i++) { //iterate for the showime
  33.             var showtime = getTimeFromString(movie.showtimes[i]); // transform a string into real time and assign to a var
  34.             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
  35.                 return "Next showing of " + movie.title + " is " + movie.showtimes[i]; //return a string with the name and showtime of the movie
  36.             } else {
  37.                 return "There´s no more showtime for" + movie.title " today. Sorry";
  38.             }
  39.         }
  40.     }
  41.  
  42. var movie1 = {
  43.     title: "Plan 9 from Outer Space",
  44.     genre: "Cult Classic",
  45.     rating: 2,
  46.     showtimes: ["3:00pm", "7:00pm", "11:00pm"],
  47.    
  48. };
  49.  
  50. var movie2 = {
  51.     title: "Forbidden Planet",
  52.     genre: "Classic Sci-fi",
  53.     rating: 5,
  54.     showtimes: ["5:00pm", "9:00pm"],
  55. };
  56.  
  57. function getTimeFromString(str) {
  58.     var theTime = new Date(); //create a date object and assign to the var theTime
  59.     var time = str.match(/(\d+)(?::(\d\d))?\s*(p?)/); //understand the string.match function and its parameters
  60.     theTime.setHours( parseInt(time[1]) + (time[3] ? 12 : 0) ); //
  61.     theTime.setMinutes( parseInt(time[2]) || 0 );
  62.     return theTime.getTime(); //understand .getTime
  63. }
Add Comment
Please, Sign In to add comment