Falvun Falvun - 22 days ago 7
Javascript Question

JavaScript. Alerting a string

I am a newbie at JavaScript. So, I was trying to solve an exercise - announcing movie showtimes. I have already checked what's wrong with it, but i found nothing. When I open the index.html file nothing happens. Here is my code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cinema</title>
<script src="cinema.js"></script>
</head>
<body>

</body>
</html>


cinema.js

var movie1={
name: "Star Wars: Force Awakens",
showtimes: ["3:00pm","7:00pm","11:00pm"],
genre: "Cult Classic",
rating: 2
};
var movie2={
name: "Doctor Strange",
showtimes: ["5:00pm","9:00pm"],
genre: "Action",
rating: 5
};

window.onload = function (){
var nextShowing=getNextShowing(movie1);
alert(nextShowing);
nextShowing=getNextShowing(movie2);
alert(nextShowing);

}
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.name+" is "+movie.showtimes[i];
}
}
return null;
}

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


Where might be a problem?

UPD:

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.name+" 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();
}


still nothing happens

Answer
  1. parseInt function name was not correct. Please replace parselnt with parseInt
  2. Match pattern was not correct. Here is the updated one

    var time = timeString.match(/(\d+)(?::)(\d+)(am|pm)/);