cinema 1

by Lucille Kenney

HTML

<p>This code demonstrates passing an object to the <em>getNextShowing</em> function that takes a <em>movie</em> as an argument.</p>
<p>But you can also use the obect to call the function. See http://jsfiddle.net/lakenney/cwk70x0t/</p>

JavaScript

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"]
};

var banzaiMovie = {
	title: "Buckaroo Banzai",
	genre: "Cult Classic",
	rating: 5,
	showtimes: ["1:00pm", "5:00pm", "7:00pm"]
};


function getNextShowing(movie) {
	var now = new Date().getTime();
	for (var i = 0; i < movie.showtimes.length; i++) {
		var showtime = getTimeFromString(movie.showtimes[i]);
		if ((showtime - now) > 0) {
			return "Next showing of " + movie.title + " is " + movie.showtimes[i];
		}
	}
	return null;
}

function getTimeFromString(str) {
	var theTime = new Date();
	var time = str.match(/(\d+)(?::(\d\d))?\s*(p?)/);
	theTime.setHours( parseInt(time[1]) + (time[3] ? 12 : 0) );
	theTime.setMinutes( parseInt(time[2]) || 0 );
	return theTime.getTime();
}

var nextShowing = getNextShowing(movie1);
console.log(nextShowing);
nextShowing = getNextShowing(movie2);
console.log(nextShowing);
nextShowing = getNextShowing(banzaiMovie);
console.log(nextShowing);