1. 简介:
本节来介绍Math函数(数学函数)中最常用的,也是比较容易出错的两个函数:max()和random(),即获取最大值和获取随机数函数。
2. max(n1,n2,n3,...):
max()函数用于获取参数中的最大值,可以有多个参数。如果想要得到一个正确的值,必须保证参数的类型都是数字或者可以转换为数字。
例子程序:
math_test.js中的max_test()函数:
function max_test(){
//case1
var res = Math.max(10,20);
console.log("case1,res=",res);
//case2
res = Math.max(10,50,40,20,35,65);
console.log("case2,res=",res);
//case3
res = Math.max(-10,-20,-5,-15);
console.log("case3,res=",res);
//case4
res = Math.max(10.5,20.5,15.5);
console.log("case4,res=",res);
//case5:无参
res = Math.max();
console.log("case5,无参,res=",res);
//case6:有NaN
res = Math.max(10,NaN,20);
console.log("case6,无参,res=",res);
//case7:有非数字
var a = "a";
res = Math.max(10,20,a);
console.log("case7,无参,res=",res);
}
测试结果(在对应的html中查看log信息):
case1,res= 20
math_test.js:8 case2,res= 65
math

最低0.47元/天 解锁文章
132

被折叠的 条评论
为什么被折叠?



