JavaScript Math()使用技巧

 时间:2024-10-12 08:13:54

1、案例一:两数比大小。我们要用JavaScript Math()对于两数进行比大小,把小的写在网页上面。

JavaScript Math()使用技巧

2、整个代码的浯裁佻辞流程如下所示:所有的小的数字就显示在网页上了。<html><body><script type="text/jav锾攒揉敫ascript">document.write(Math.min(1,2) + "<br />")document.write(Math.min(-1,3) + "<br />")document.write(Math.min(-5,-7) + "<br />")document.write(Math.min(7.22,7.30))</script></body></html>

JavaScript Math()使用技巧

3、我们如果把min 改成 max 则结果就完全不同了。<html><body><script type="text/javascript">document.write(Math.max(1,2) + "<br />")document.write(Math.max(-1,3) + "<br />")document.write(Math.max(-5,-7) + "<br />")document.write(Math.max(7.22,7.30))</script></body></html>

JavaScript Math()使用技巧

4、一个随机数字的案例。每一次的点击,这个网页上的数字都不一样。<html><body><script type="text/javascript">document.write(Math.random())</script></body></html>

JavaScript Math()使用技巧

5、我们聪艄料姹再来看一批不同算法的案例。这里有2的平方根、圆周率、常数等等。<html><body><script type="text/javascript媪青怍牙">document.write(Math.SQRT2 + "<br />")document.write(Math.PI + "<br />")document.write(Math.E + "<br />")document.write(Math.SQRT1_2 + "<br />")document.write(Math.LN2)</script></body></html>

JavaScript Math()使用技巧

6、这是一个把两种算法用在一起的代码流程。每一次有结果都不一样!<html><body><script type="text/javascript">document.write(Math.floor(Math.random()*11))</script></body></html>

JavaScript Math()使用技巧
  • JavaScript 使用技巧解析
  • JavaScript 函数的使用技巧
  • javascript浅谈
  • javascript的特点
  • javascript this的用法
  • 热门搜索
    竹笋怎么处理 新款捷达车怎么样 高光怎么打 桑塔纳浩纳怎么样 黑眼圈很重怎么办 云南师范大学怎么样 惠达卫浴怎么样 精子不液化怎么办 重庆大学城市科技学院怎么样 豆腐乳怎么做