Jump to content
Larry Ullman's Book Forums

Chapter 12 Assertion


Recommended Posts

Hello,

 I am new to the programming and javascript and I have a question. I can not figure out how to get assertion function to work. I tried it with the sphere.js that is included with the book but it does not work.

once I run the code nothing happens, but if I comment out the radius assertion it works fine.

Thanks for the help.

 

//script

function assert(expression, message) {

if (!expression) throw { name: 'Assertion Exception', message: message};
}
 
function calculate() {
'use strict';
 
// For storing the volume:
var volume;
    
    // Get a reference to the form value:
    var radius = document.getElementById('radius').value;
 
    assert((typeof radius == 'number'), 'The radius has to be a number.');
    
// Perform the calculation:
volume = (4/3) * Math.PI * Math.pow(radius, 3);
 
assert(!isNaN(volume), 'The volume is not a number');
// Format the volume:
volume = volume.toFixed(4);
 
// Display the volume:
document.getElementById('volume').value = volume;
 
// Return false to prevent submission:
return false;
    
} // End of calculate() function.
 
// Function called when the window has been loaded.
// Function needs to add an event listener to the form.
function init() {
'use strict';
document.getElementById('theForm').onsubmit = calculate;
} // End of init() function.
window.onload = init;
Link to comment
Share on other sites

It throws an error:

 

Uncaught #<Object>   sphere.js:6

assert                           sphere.js:6 

calculate                      sphere.js:21 

I'm trying to figure it out, searching the web, but I can't seem to figure out the solution.

Link to comment
Share on other sites

  • 3 weeks later...
 Share

×
×
  • Create New...