![]() |
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> |
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. |
Quote:
|
And in any case, this line is *NOT* valid JavaScript:
Code:
Declare Numeric totCust // total CustomersIn JavaScript, you would just write Code:
var totCust; // total CustomersI 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> |
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 |
Quote:
Mike |
Quote:
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 |
Quote:
Mike |
Quote:
|
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>Code:
</script>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. |
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. |
Quote:
Quote:
Quote:
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. Quote:
Code:
<script type="text/javascript"> |
| All times are GMT +1. The time now is 02:32 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.