Jump to content
Larry Ullman's Book Forums

Ch.5 Modified Sphere Program


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 comment
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 comment
Share on other sites

 Share

×
×
  • Create New...