Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    functions and objects

    Hello,
    I am working through a book learning JavaScript and one of the examples does not work. I have no idea why. The task is simply for an alert message to appear telling the user the next time a film will be starting. Sorry for the long code but I cannot see where the error/ Omission is.
    All help greatly appreciated
    Code:
    <!doctype html>
    <html lang="en">
    <head>
    <title>Webville Cinema</title>
    <meta charset="utf-8">
    <script>
    
    var movie1 = {
    	title: "Plan 9 from Outer Space",
    	genre: "Cult Classic",
    	rating: 5,
    	showitmes: ["3:30pm", "7:00pm", "23:30pm"]
    };
    
    var movie2 = {
    	title: "Forbidden Planet",
    	genre: "Classic Sci-fi",
    	rating: 2,
    	showtimes: ["5:00pm", "9:00pm"]
    };
    
    
    function getNextShowing(movie) {
    	var now = new Date().getT‪ime();
    
    	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);
    nextShowing = getNextShowing(movie2);	
    	alert(nextShowing);
    
    </script>
    </head>
    <body>
    </body>
    </html>

  • #2
    0x3
    0x3 is offline
    New Coder
    Join Date
    Sep 2013
    Posts
    37
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Fiddling around;
    PHP Code:
    var now = new Date().getT&#8234;ime();
    console.log(now);
    // Uncaught SyntaxError: Unexpected token ILLEGAL 
    var time timeString.match(/(d+) (?::(dd))?s*(p?)/);
    console.log(time);
    // Uncaught SyntaxError: Unexpected identifier 
    there are a couple of problems to fix. :P

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Forgive me...I added the lines in. Literally nothing happens. At all. I have a blank screen!

  • #4
    0x3
    0x3 is offline
    New Coder
    Join Date
    Sep 2013
    Posts
    37
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by dp76 View Post
    Forgive me...I added the lines in. Literally nothing happens. At all. I have a blank screen!
    Those are errors, The Regex is has an error, non-capturing syntax is (?expression)) not (?: The other error is related to an illegal operation.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Code:
    function getTimeFromString(timestring) {
    	var theTime = new Date();
    	var time = timeString.match(/(\d+) (?::(\d\d))?\s*(p?)/);
    those are two different variables. (JavaScript is case-sensitive)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You also always want to pass the base to parseInt, which typically would be 10, i.e. parseInt(something, 10) – otherwise you leave it up to parseInt to decide on the base, which can go very wrong.

  • #7
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by dp76 View Post
    Code:
    var movie1 = {
    	title: "Plan 9 from Outer Space",
    	genre: "Cult Classic",
    	rating: 5,
    	showitmes: ["3:30pm", "7:00pm", "23:30pm"]
    };
    The property name showtimes is misspelled.

    Quote Originally Posted by dp76 View Post
    Code:
    function getNextShowing(movie) {
    	var now = new Date().getT‪ime();
    You have an invisible control character, U+202A LEFT-TO-RIGHT EMBEDDING, nested in this code. You can see the location of the character in 0x3's first post where it's represented as an HTML character escape in decimal notation (&#8234;). The easiest way to get rid of it is to retype this code on another line and then delete the original line.

    Alternatively, you can download a character editor like BabelMap, paste the offending snippet into the character buffer at the bottom of the window, switch to "NCR (hex)" mode using a radio button at the bottom to see an escaped version of the character, and then delete the escape representing the character. (This is more effort, but I mention the program because it has a lot of utility in being able to literally see which characters are in use which is quite useful when you have characters that look similar like -,‐, and – or invisible characters like this one.)

    Quote Originally Posted by dp76 View Post
    Code:
    		var showtime = getTimeFromString(movie,showtimes[i]);
    Two arguments have been passed to the function getTimeFromString, but the function is only defined to take one.

    Quote Originally Posted by dp76 View Post
    Code:
    function getTimeFromString(timestring) {
    	var theTime = new Date();
    	var time = timeString.match(/(\d+) (?::(\d\d))?\s*(p?)/);
    The reference timeString is is not a case-sensitive match for the timestring parameter. (Dormilich covered this.)

    Quote Originally Posted by 0x3 View Post
    Those are errors, The Regex is has an error, non-capturing syntax is (?:(expression)) not (?::)
    That's not an error. The colon after the no-capture token (?:) indicates that a colon character should be matched.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    You are not trying to change numbers from one base to another so there's no reason to use parseInt - the function to call to convert strings to numbers is Number()
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    0x3
    0x3 is offline
    New Coder
    Join Date
    Sep 2013
    Posts
    37
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Arbitrator View Post
    That's not an error. The colon after the no-capture token (?:) indicates that a colon character should be matched.
    Correct, its not an error, at first i thought so, but it does work, with or without escaping.

  • #10
    New Coder
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for all help..nothing works..blank screen! I would prefer it if I actually got an error message. but there is nothing!

  • #11
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Code:
     
    <!doctype html>
    <html>
    <head>
    <title>Webville Cinema</title>
    <meta charset="utf-8">
    <script>
    var movie1 = {
     title: "Plan 9 from Outer Space",
     genre: "Cult Classic",
     rating: 5,
     showtimes: ["3:30pm", "7:00pm", "23:30pm"]//showitmes
    };
    var movie2 = {
     title: "Forbidden Planet",
     genre: "Classic Sci-fi",
     rating: 2,
     showtimes: ["5:00pm", "9:00pm"]
    };
    
    function getNextShowing(movie) {
     var now = new Date().getTime();//getT?ime
     for (var i = 0; i < movie.showtimes.length; i++) {
      var showtime = getTimeFromString(movie.showtimes[i]);//movie,showtimes
      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?)/i); //match(/(\d+) (?::(\d\d))?\s*(p?)/);//timeString
     theTime.setHours( parseInt(time[1]) + (time[3] ? 12 : 0) );
     theTime.setMinutes( parseInt(time[2]) || 0 );
     return theTime.getTime();
    }
    var nextShowing = getNextShowing(movie1);
     alert(nextShowing);
    nextShowing = getNextShowing(movie2); 
     alert(nextShowing);
    </script>
    </head>
    <body>
    </body>
    </html>

  • Users who have thanked DaveyErwin for this post:

    dp76 (09-10-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •