arthemg 0 Posted April 3, 2015 Report Share Posted April 3, 2015 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; Quote Link to post Share on other sites
Larry 429 Posted April 3, 2015 Report Share Posted April 3, 2015 Did you check the console for any JavaScript errors? Quote Link to post Share on other sites
arthemg 0 Posted April 3, 2015 Author Report Share Posted April 3, 2015 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. Quote Link to post Share on other sites
Larry 429 Posted April 22, 2015 Report Share Posted April 22, 2015 I'm so sorry for the delayed reply; this slipped through the cracks. Are you still having issues with this? Quote Link to post Share on other sites
Recommended Posts
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.