Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
arthemg

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;

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

I'm so sorry for the delayed reply; this slipped through the cracks. Are you still having issues with this?

Share this post


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.

Guest
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.

Loading...
Sign in to follow this  

×
×
  • Create New...