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;