JavaScript产生随机数并对数据进行简单的运算

<!DOCTYPE html>
<html>
<head>
<title>随机产生整数,并计算其平方、平方根和自然对数</title>
<script>
var data;

function getRandom(){
     data=Math.floor(Math.random()*101);
     alert("随机整数为:"+data);
}
function cal(){
     var square=Math.pow(data,2);
	 var squareRoot=Math.sqrt(data).toFixed(2);
	 var logarithm=Math.log(data).toFixed(2);
	 alert("随机整数"+data+"的相关计算\n平方  \t平方根  \t自然对数  \n"+square+"\t           "+squareRoot+"\t   "
	 +logarithm);
}
</script>
</head>
<body>
<form action="" method="post" name="myform" id="myform">
<input type="button" value="随机数" οnclick="getRandom()">
<input type="button" value="计  算" οnclick="cal()">
</form>
</body>
</html>

posted @ 2015-07-20 20:37  wojiaohuangyu  阅读(7)  评论(0)    收藏  举报