JohnS Posted May 29, 2015 Share Posted May 29, 2015 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 More sharing options...
Larry Posted May 30, 2015 Share Posted May 30, 2015 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 More sharing options...
Recommended Posts