Reputation: 11
Please, look at this code and tell me why doesn't work? i think that there is a problem with return in function. Thanks for all replays.
var movie1 = {
title: "Buckaroo Banzai",
genre: "Cult classic",
rating: 5,
showtimes: ["1:00pm", "3: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(timeString) {
var theTime = new Date();
var time = timeString.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);
alert(nextShowing);
Upvotes: 0
Views: 151
Reputation: 1
I tested the above code, and it only returned me a null when the time in my computer is greater than or equal to 7:00pm.
Otherwise it works fine. Try changing the timing of the showtimes array, so that they are ahead of current time in your computer. Or try running it during the morning :). I think that is all you need to do.
Upvotes: 0
Reputation: 61793
The issue is with this line of code:
if ((showtime - now) > 0)
(showtime - now)
is always less than zero therefore the getNextShowing()
function always returns null
;
Upvotes: 1