March 28, 2024

Answer to JavaScript Exercise Q4

 

//declare variables
var num1, num2, result;
/*
declare and initialise some more variables
and assign the input from two dialogue boxes
*/
var numStr1 = window.prompt('Enter a number', '');
var numStr2 = window.prompt('Enter another number', '');
//convert both string variables to numbers
num1 = parseFloat(numStr1);
num2 = parseFloat(numStr2);
result = num1/num2;
//round the result
result = Math.round(result);
//output a suitable message
document.write('The rounded division of ' + numStr1 + ' by '
+ numStr2 + ' is:' + '<BR>');
//convert the product to a string and output the value
document.write(result);
//also
//document.write(result.toString());

Notes:

  • This makes use of the JavaScript Math library and the round() method, which takes a number argument and rounds, up or down, to the nearest integer value.

 

 

 

 

 

 

 

 

 

Back « JavaScript numbers

Up to top of page