Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2013, 08:50 PM   PM User | #1
Modify_inc
New Coder

 
Join Date: Jan 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Modify_inc is an unknown quantity at this point
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 09:10 PM..
Modify_inc is offline   Reply With Quote
Old 01-12-2013, 08:58 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,460
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 01-12-2013, 09:17 PM   PM User | #3
Modify_inc
New Coder

 
Join Date: Jan 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Modify_inc is an unknown quantity at this point
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.
Modify_inc is offline   Reply With Quote
Old 01-12-2013, 09:19 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,232
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
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.

Last edited by Old Pedant; 01-12-2013 at 09:25 PM..
Old Pedant is online now   Reply With Quote
Old 01-12-2013, 09:22 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,232
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 01-12-2013, 09:44 PM   PM User | #6
Modify_inc
New Coder

 
Join Date: Jan 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Modify_inc is an unknown quantity at this point
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
Modify_inc is offline   Reply With Quote
Old 01-12-2013, 10:18 PM   PM User | #7
Modify_inc
New Coder

 
Join Date: Jan 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Modify_inc is an unknown quantity at this point
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
Modify_inc is offline   Reply With Quote
Old 01-12-2013, 10:24 PM   PM User | #8
Modify_inc
New Coder

 
Join Date: Jan 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Modify_inc is an unknown quantity at this point
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
Modify_inc is offline   Reply With Quote
Old 01-12-2013, 10:44 PM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,460
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 01-13-2013, 12:19 AM   PM User | #10
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 01-13-2013, 05:17 PM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 01-13-2013, 07:46 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,232
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.

Quote:
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.
Quote:
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.

Quote:
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.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:45 AM.


Advertisement
Log in to turn off these ads.