老师,我取到值了为什么最大最小都是NAN

来源:4-8 编程练习

精慕门2504353

2020-04-05 20:21:25

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Math.random()</title>

</head>

<body>

<script>

var num=new Array();

for(var i=0;i<10;i++){

    num[i]=Math.floor(Math.random()*100+1);

}

document.write(num);

max=Math.max(num);

min=Math.min(num);

s=num.sort(function(a,b){return b-a});

document.write("倒序:"+s+"<br>");

document.write("这组数字中最大的值是:"+max);

document.write("最小的值是:"+min)

</script>

</body>

</html>


写回答

1回答

好帮手慕夭夭

2020-04-06

同学你好, Math.max和Math.min不能直接传入数组,这里num就是一个数组,传入之后,数组返回的就是NaN.

http://img.mukewang.com/climg/5e8aec2409505cc504940151.jpg

如果这里要获取最大值和最小值,可以使用数组下标方式。如下:

http://img.mukewang.com/climg/5e8aec6f09e1b00106820332.jpg

如果我的回答帮助到了你,欢迎采纳,祝学习愉快~

0

0 学习 · 40143 问题

查看课程