arthemg Posted April 3, 2015 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; Link to comment Share on other sites More sharing options...
Larry Posted April 3, 2015 Share Posted April 3, 2015 Did you check the console for any JavaScript errors? Link to comment Share on other sites More sharing options...
arthemg Posted April 3, 2015 Author 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. Link to comment Share on other sites More sharing options...
Larry Posted April 22, 2015 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? Link to comment Share on other sites More sharing options...
Recommended Posts