Jump to content
Larry Ullman's Book Forums

Recommended Posts

On p.138  the sphere program is modified. In step 2, he says to change the assignment to: 

var radius = document.getElementById('radius');

and in step 3, to use:

if (radius && (radius.value > 0)) {

What is the point of this extra complication? In other words, why not just use the original:

var radius = document.getElementById('radius').value;

and then simply test for:

if (radius > 0) {

I've written versions both his way and my way, and they both work the same. I don't understand what he means by: 'The first part of this condition confirms that the radius variable has a TRUE value. So long as the document.getElementById() method was able to find an element in the page that has an id of radius, this will be the case.'

We know that the radius field exists because we wrote it into the form in our HTML file. Why would we have to test whether the field exists?

Link to post
Share on other sites

The extra complication as you describe it is a matter of being precise and not making assumptions, which are generally good things. And, yes, while you know the field exists because you wrote it, that assumes you didn't make a mistake and/or that the end user didn't try to manipulate things. 

So, in short, I guess I'd differentiate between "what works" and "what makes the fewest assumptions". 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...