Hello, I need in help in javascript. I want to create a script that accepts a nu
ID: 3716974 • Letter: H
Question
Hello,
I need in help in javascript. I want to create a script that accepts a number with atleast four decimal points. An error message if it is less. I want the original number to be displayed, the number rounded to integer, square root(that is then rounded), rounded to nearest 20th, 200th, and 2000th. I also need parseFloat() to be used. Results should be displayed in a text area and user enters info in a form box. If you could use fixed for the numbers, that would be appreciated as well. Thank you ! Comments and output appreciated.
Explanation / Answer
<!DOCTYPE Html>
<html>
<head>
<title> Test</title>
<script type="text/javascript">
function myFunc(){
var userVal=parseFloat(document.getElementById("nbr").value);
var n = userVal.toString().split(".");
var decPt= n.length > 1 ? n[1].length : 0;
if(decPt!=4){
alert("Please enter a 4 decimal point number !!!");
return;
}
opdiv = document.getElementById("output");
opdiv.innerHTML="";
opdiv.innerHTML+= " Original Number : " + userVal.toString();
opdiv.innerHTML+=" <hr>"
var round = Math.round(userVal).toString();
opdiv.innerHTML+= " Rounded Number : " + round;
opdiv.innerHTML+=" <hr>"
var sqrt=Math.sqrt(round).toString();
opdiv.innerHTML+= " Square Root of "+ round +" : " + sqrt ;
opdiv.innerHTML+=" <hr>"
// the question about rounding to 20th , 200th is not clear.. hope i have understood correctly;
var r20=Math.ceil(sqrt/20)*20;
var r200=Math.ceil(sqrt/200)*200;
var r2000=Math.ceil(sqrt/2000)*2000;
opdiv.innerHTML+= "Square Root Rounded to 20th: " + r20.toString() ;
opdiv.innerHTML+=" <hr>"
opdiv.innerHTML+= " Square Root Rounded to 200th: " + r200.toString() ;
opdiv.innerHTML+=" <hr>"
opdiv.innerHTML+= " Square Root Rounded to 2000th: " + r2000.toString() ;
opdiv.innerHTML+=" <hr>"
}
</script>
</head>
<body>
<div>
Enter a number with atleast 4 decimal points: <input type="text" id="nbr">
<input type="button" value="Go!">
</div>
<div id ="output"></div>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.