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 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Taking a Class in JavaScript: How do you test your code?

    Hey ALL

    I just started taking a class that teaches the basics of writing JavaScript. I'm in the first chapter of the book, Problem-Solving with JavaScript, 1st Edition by Addison. I have read bad reviews of this book, that much of the code that is referenced is outdated, then again I've heard that many are bad that cover JavaScripting, except for the one that has a Rhino on the cover (I forget the name).

    Apparently this book just covers writing JavaScript (at least in the 1st chapter) because it does not discussed how to run the programs after you create them. With that, I have no idea how to test my beginners code. It informs you to open notepad, and save the code as a text file. Now this can't be right, as I know text is not executable.

    I'm using Notepad++ and it gives you the option to run from your browser, but doing that only displays the file contents. I tried saving it as a .js and launching it again in the browser, but same thing, just displayed the contents of the file.

    Executing it from Windows 7, using Windows Scripting, generates a scripting error at Line 7, Char 9, Expected ';' Code 800A03EC. This is line 7:

    Declare Numeric totCust // total Customers

    I verified the syntax of the line to be correct from the book, so either Windows Scripting Host does not agree with it or maybe it's true, the JavaScript the book is teaching is outdated.

    To be honest I have no idea, that's why I'm posting.

    Thanks
    Mike

    Update:
    I tried running it by including it in a html file and the JavaScript still did not execute. I only know basic html, but this syntax should be correct. Should note that the html does run, as the Testing JavaScript is displayed in my browser.

    <html>
    <head>
    Testing JavaScript
    <script type="text/javascript" src="F:\PortableApps\Notepad++Portable\customerStats.js"></script>
    </head>
    </html>
    Last edited by Modify_inc; 01-12-2013 at 10:10 PM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Most JavaScript runs in a web page - you attach the JavaScript into the web page using the HTML <script> tag.

    See http://javascriptexample.net/basics01.php for a basic example of how to attach JavaScript to a web page so that it runs when the web page loads.
    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.

  • #3
    New Coder
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    Most JavaScript runs in a web page - you attach the JavaScript into the web page using the HTML <script> tag.

    See http://javascriptexample.net/basics01.php for a basic example of how to attach JavaScript to a web page so that it runs when the web page loads.
    You posted after I had edited my post, but that didn't work either. It does not give an error, just a blank white screen in my browser. I have no idea why it is not running.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,685
    Thanks
    80
    Thanked 4,646 Times in 4,608 Posts
    And in any case, this line is *NOT* valid JavaScript:
    Code:
    Declare Numeric totCust	 // total Customers
    JavaScript does not use a Declare statement to define variables. Also, you can't even tell JavaScript what the data type of a variable is when it is created (among other reasons, because the type can be changed at any time).

    In JavaScript, you would just write
    Code:
    var totCust; // total Customers
    I strongly suspect this is the idiotic book that tries to first teach you a PSEUDO-LANGUAGE (supposedly to make it easier to think about HOW to solve problems) and then has you convert the pseudo-language into JavaScript.

    I feel sorry for you, that your stupid stupid instructor would choose this horrible book out of all those available.

    And, yes, you can use Windows Script Host to execute pure JavaScript files, but then the way you ask the user to enter values and the way you display results are *COMPLETELY* different than you would use in a Web browser and so you will just have to un-learn all the WSH stuff when you start using JS in a web-based system. On top of that, debugging in the WSH environment is a pain in the neck. At a minimum, you need to download the Windows Script Debugger and you probably should instead use Visual Web Developer Express. A lot of overhead for a little debug help.

    So you are better off just writing HTML code and then debugging it in a web browser.

    All modern browsers except FireFox come with a debugger built in (and FireBug can be plugged into FireFox and used as its debugger). All of them can be invoked by simply hitting the F12 key.

    MSIE 9 is not too bad for debugging, but I think that CHROME is a better choice. There are just a handful of differences, but that handful are enough to make using Chrome much easier, especially when just beginning.

    And while you *CAN* create your JS code in a ".js" file and then attach it to a web page as Felgall shows (and as you will want to do when you get more advanced), especially when you are just starting this is more of a pain than it is worth. Instead, just embed your JS code right into your HTML code. Just use
    Code:
    <html>
    <body>
    <script type="text/javascript">
    ... put your js code here ...
    </script>
    </body>
    </html>
    Later, you will learn when to put the JS code into the <head>...</head> section of the HTML and when to leave it where I show it there. But given what we know about that atrocious book, the way I show it there is likely the best starting spot.
    Last edited by Old Pedant; 01-12-2013 at 10:25 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,685
    Thanks
    80
    Thanked 4,646 Times in 4,608 Posts
    Just checked Amazon: Yep, this is maybe the worst book for learning JavaScript to come along in recent years. Read the 3 reviews here:
    http://www.amazon.com/Principles-Pro...owViewpoints=1
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Just checked Amazon: Yep, this is maybe the worst book for learning JavaScript to come along in recent years. Read the 3 reviews here:
    http://www.amazon.com/Principles-Pro...owViewpoints=1
    Yes that's where I was originally informed. To be honest I have not purchased the book, because I can't justify spending that kind of money on it knowing what I now know about it. I tried looking online for a e-version of it and actually found a good one, but it only covers the first full chapter. Just my luck, but it does give me time to find a cheaper version.

    Mike

  • #7
    New Coder
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    And in any case, this line is *NOT* valid JavaScript:
    Code:
    Declare Numeric totCust	 // total Customers
    JavaScript does not use a Declare statement to define variables. Also, you can't even tell JavaScript what the data type of a variable is when it is created (among other reasons, because the type can be changed at any time).

    In JavaScript, you would just write
    Code:
    var totCust; // total Customers
    I strongly suspect this is the idiotic book that tries to first teach you a PSEUDO-LANGUAGE (supposedly to make it easier to think about HOW to solve problems) and then has you convert the pseudo-language into JavaScript.

    I feel sorry for you, that your stupid stupid instructor would choose this horrible book out of all those available.

    And, yes, you can use Windows Script Host to execute pure JavaScript files, but then the way you ask the user to enter values and the way you display results are *COMPLETELY* different than you would use in a Web browser and so you will just have to un-learn all the WSH stuff when you start using JS in a web-based system. On top of that, debugging in the WSH environment is a pain in the neck. At a minimum, you need to download the Windows Script Debugger and you probably should instead use Visual Web Developer Express. A lot of overhead for a little debug help.

    So you are better off just writing HTML code and then debugging it in a web browser.

    All modern browsers except FireFox come with a debugger built in (and FireBug can be plugged into FireFox and used as its debugger). All of them can be invoked by simply hitting the F12 key.

    MSIE 9 is not too bad for debugging, but I think that CHROME is a better choice. There are just a handful of differences, but that handful are enough to make using Chrome much easier, especially when just beginning.

    And while you *CAN* create your JS code in a ".js" file and then attach it to a web page as Felgall shows (and as you will want to do when you get more advanced), especially when you are just starting this is more of a pain than it is worth. Instead, just embed your JS code right into your HTML code. Just use
    Code:
    <html>
    <body>
    <script type="text/javascript">
    ... put your js code here ...
    </script>
    </body>
    </html>
    Later, you will learn when to put the JS code into the <head>...</head> section of the HTML and when to leave it where I show it there. But given what we know about that atrocious book, the way I show it there is likely the best starting spot.
    Thanks for your input, and your opinion of the book. I believe you may be right about the book teaching a Pseudo-Language and then translating it later in the book. I have went through the code and added everything the WSH reported an error on and thought that I might finally get to see the JavaScript execute the code, but then WSH reported another subset of errors starting from the beginning again with "undefined" for Start and a simple Display command.

    Is Start not required anymore, as the book states to use it? Does undefined mean the command is not recognized anymore?

    To display and input text, the following commands are mentioned:

    Display "Enter the # of customers for the first hour: "
    Input count1

    Also there is a Declare Constant NUM_HOURS = 4, I assume that since you stated Declare is invalid that I need to change this but wasn't sure how to go about it since it was a Constant, so I just swapped it for var which I'm sure is perfectly find in a test script of this nature.

    I installed Firebug and it stated that it found no JavaScript in my html.

    Mike

  • #8
    New Coder
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Just checked Amazon: Yep, this is maybe the worst book for learning JavaScript to come along in recent years. Read the 3 reviews here:
    http://www.amazon.com/Principles-Pro...owViewpoints=1
    That is where I was originally informed when I was looking for the book. I have not purchased the book because after reading those reviews, I didn't want to spend that kind of money on it. I searched the internet for a e-version of the book, and I found a portion of the book that covers the first chapter in full along with the activities that the instructor requires from the first chapter. At least this gives me some extra time to find a better deal on the book, though I'm not having much luck.

    Mike

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Modify_inc View Post
    Is Start not required anymore, as the book states to use it? Does undefined mean the command is not recognized anymore?

    To display and input text, the following commands are mentioned:

    Display "Enter the # of customers for the first hour: "
    Input count1
    There are no such JavaScript commands as Start, Declare, Display or Input - the debugger is correct in saying that it found no JavaScript in your page because none of that text is JavaScript.
    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.

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Congratulations, javascript is king: welcome to the rest of your life.

    you can use notepad and test it in a a browser, with a tiny bit of boilerplate.

    add this line first thing:
    Code:
    <script>
    and this line as the last line of the document:
    Code:
    </script>
    save it test.html and it should run in any browser.


    if you want to shortcut all that, feel free to tryout your code snips in the CODE box at http://danml.com/sandbox/ , and then click run to run. The environment will allow you to save your code, share it with others, and also provides a few tools like empty textboxes to paste input data.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    You should note the comments in one of the reviews:-

    Any instructor who chooses this book to teach a MODERN class in JavaScript should be ashamed of herself/himself. Students assigned this book should protest to the school that they are being taught techniques that are so far out of date as to make the instruction worthless in a contemporary environment.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,685
    Thanks
    80
    Thanked 4,646 Times in 4,608 Posts
    Quote Originally Posted by Modify_inc View Post
    I believe you may be right about the book teaching a Pseudo-Language and then translating it later in the book.
    "May" be right? Clearly we *ARE* right.

    Is Start not required anymore, as the book states to use it? Does undefined mean the command is not recognized anymore?
    Not "any more." It *NEVER* was part of JavaScript. NEVER NEVER NEVER. It has *NOTHING* to do with JavaScript. ONE MORE TIME: It is part of the idiotic pseudocode language Mr. Addled Addison created.
    To display and input text, the following commands are mentioned:
    Display "Enter the # of customers for the first hour: "
    Input count1
    Also there is a Declare Constant NUM_HOURS = 4
    And yet again, one more time: *ALL* of those statements and keywords are *NOT* part of JavaScript. *AT ALL*. They belong *ONLY* to that essentially WORTHLESS pseudocode language that the idiot author invented.

    There is a very very famous set of books from the early 1960s that teach the *theory* of programming. Back then, nobody (or almost nobody) used any of the languages we use most frequently today. So Donald Knuth created a pseudo-code language to teach programming algorithms and techniques. It made sense: He wanted people to be able to use the IDEAS he was presenting no matter what computer language they chose to actually write in.

    But that concept makes NO SENSE AT ALL when an author is trying to teach how to program a *SINGLE* programming language, in this case JavaScript. I don't care how "respected" Mr. Addison is, he sure can't seem to write a USABLE tutorial book. As those reviews all say, his idiotic technique simply DOES NOT WORK. And, beyond that, he doesn't even try to teach *MODERN* JavaScript. Read what Philip said: He is dead on right.

    I installed Firebug and it stated that it found no JavaScript in my html.
    Which probably means you didn't follow the pattern I showed you. You *MUST* use the
    Code:
    <script type="text/javascript">
    ... your javascript code goes here ...
    </script>
    pattern. If you don't have a <script> tag, the browser WILL NEVER EVEN ATTEMPT to see/understand your code as being JavaScript.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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