作业一:
随机输[20,80]出区间内数。
作业二:
拓展:四舍五入计算之后,保留小数点之后的两位数
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>作业</title>
<script type="text/javascript">
// 作业一:取[20,80]之间的随机数
document.write("<h4 style='text-align: center;'>作业一</h4>");
var random = Math.random() * 61 + 20;
if (random >= 80) {
document.write(Math.floor(random) + "<br />");
} else{
document.write(random + "<br /><br />");
}
// 作业二:保留两位小数
document.write("<h4 style='text-align: center;'>作业二</h4>");
// 1.定义四舍五入函数
// PS:num为要进行保留的数;roundNum为要保留的位数
function roundFun(num, roundNum) {
var round = num.toFixed(roundNum);
document.write(round + "<br />");
// 2.基本要求:保留两位
roundFun(7.934, 2);
roundFun(7.9344, 2);
roundFun(7.9346, 2);
roundFun(2.785, 2);
roundFun(2.7854, 2);
roundFun(2.7856, 2);
// 3.更高拓展:保留多位,但最多只可以保留20位
roundFun(33.3333, 3);
roundFun(88.8888, 3);
</script>
</head>
<body>
</body>
</html>
function roundBy(num, roundNum) {
var round = Math.round(num * Math.pow(10, roundNum)) / Math.pow(10, roundNum);
roundBy(7.934, 2);
roundBy(7.9344, 2);
roundBy(7.9346, 2);
roundBy(2.785, 2);
roundBy(2.7854, 2);
roundBy(2.7856, 2);
roundBy(33.3333, 3);
roundBy(88.8888, 3);
roundBy(3.141592653589793238462643383279502884197169399375105820974944, 30);
效果是一样的,上面提到,最后一个实际输出3.141592653589793
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
document.write("作业一:[20 80]的随机数"+"<br/><br/>");
var ra=Math.random()*61+20;
if(ra>80){
var random;
random=Math.floor(ra);
}else{
random=ra;
document.write(random+"<br/><br/>");
document.write("作业二:四舍五入保留小数位"+"<br/><br/>");
function myMath(num,digit){
var round=Math.round(num*Math.pow(10,digit))/Math.pow(10,digit);
document.write("保留"+digit+"位小数:"+round+"<br/>");
myMath(ra,2);
myMath(ra,3);
myMath(ra,4);
myMath(ra,5);
var random = (Math.random()*60+19)+1 ;
if (random >=20) {
document.write("yes,大于等于20"+"<br />");
document.write("no,答案错误"+"<br />");
if (random <=80) {
document.write("yes,小于等于80"+"<br />")
document.write("no,答案错误")
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>作业</title>
<script type="text/javascript">
// 作业一:取[20,80]之间的随机数
document.write("<h4 style='text-align: center;'>作业一</h4>");
var random = Math.random() * 61 + 20;
if (random >= 80) {
document.write(Math.floor(random) + "<br />");
} else{
document.write(random + "<br /><br />");
}
// 作业二:保留两位小数
document.write("<h4 style='text-align: center;'>作业二</h4>");
// 1.定义四舍五入函数
// PS:num为要进行保留的数;roundNum为要保留的位数
function roundFun(num, roundNum) {
var round = num.toFixed(roundNum);
document.write(round + "<br />");
}
// 2.基本要求:保留两位
roundFun(7.934, 2);
roundFun(7.9344, 2);
roundFun(7.9346, 2);
roundFun(2.785, 2);
roundFun(2.7854, 2);
roundFun(2.7856, 2);
// 3.更高拓展:保留多位,但最多只可以保留20位
roundFun(33.3333, 3);
roundFun(88.8888, 3);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>作业</title>
<script type="text/javascript">
function roundBy(num, roundNum) {
var round = Math.round(num * Math.pow(10, roundNum)) / Math.pow(10, roundNum);
document.write(round + "<br />");
}
roundBy(7.934, 2);
roundBy(7.9344, 2);
roundBy(7.9346, 2);
roundBy(2.785, 2);
roundBy(2.7854, 2);
roundBy(2.7856, 2);
roundBy(33.3333, 3);
roundBy(88.8888, 3);
roundBy(3.141592653589793238462643383279502884197169399375105820974944, 30);
</script>
</head>
<body>
</body>
</html>
效果是一样的,上面提到,最后一个实际输出3.141592653589793
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
<script type="text/javascript">
document.write("作业一:[20 80]的随机数"+"<br/><br/>");
var ra=Math.random()*61+20;
if(ra>80){
var random;
random=Math.floor(ra);
}else{
random=ra;
}
document.write(random+"<br/><br/>");
document.write("作业二:四舍五入保留小数位"+"<br/><br/>");
function myMath(num,digit){
var round=Math.round(num*Math.pow(10,digit))/Math.pow(10,digit);
document.write("保留"+digit+"位小数:"+round+"<br/>");
}
myMath(ra,2);
myMath(ra,3);
myMath(ra,4);
myMath(ra,5);
</script>
</head>
<body>
</body>
</html>
<script type="text/javascript">
var random = (Math.random()*60+19)+1 ;
if (random >=20) {
document.write("yes,大于等于20"+"<br />");
} else{
document.write("no,答案错误"+"<br />");
}
if (random <=80) {
document.write("yes,小于等于80"+"<br />")
} else{
document.write("no,答案错误")
}
</script>
"+"yes,大于等于20,小于等于80"+"
"); } else{ document.write("no,答案错误"+"
"); }