我在 FreeCodeCamp 中发现,要解决获取某个范围内的随机整数的问题,可以使用Math.floor. 四舍五入不准确。它返回等于或小于。这不是我想的那样。
这是给定的公式:
Math.floor(Math.random() * (max - min + 1)) + min
有谁知道为什么它更用于四舍五入到最接近的整数?
提前致谢!
我被介绍到以下代码:
<!DOCTYPE html>
<html>
<head>
<style>
span {
color: blue;
border: 1px solid black;
}
.extra span {
color: inherit;
}
</style>
</head>
<body>
<div>
Here is <span>a span element</span> which is blue, as span elements are set to be.
</div>
<div class="extra" style="color:green">
Here is <span>a span element</span> which is green, because it inherits from its parent.
</div>
<div style="color:red">
Here is <span>a span element</span> which is blue, as span elements are set to be.
</div>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
链接: …