简体   繁体   English

为什么这个javascript代码有效?

[英]Why does this javascript code work?

    <!doctype html>
<html lang="en">
<head>
<title>Phrase-o-matic</title>
<meta charset="utf-8">
<style>
body {
    font-family: Verdana, Helvetica, sans-serif;
}
</style>
<script>

function makePhrases() {
    var words1 = ["24/7", "multi-Tier", "30,000 foot", "B-to-B", "win-win"];
    var words2 = ["empowered", "value-added", "oriented", "focused", "aligned"];
    var words3 = ["process", "solution", "tipping-point", "strategy", "vision"];

    var rand1 = Math.floor(Math.random() * words1.length);
    var rand2 = Math.floor(Math.random() * words2.length);
    var rand3 = Math.floor(Math.random() * words3.length);

    var phrase = words1[rand1] + " " + words2[rand2] + " " + words3[rand3];
    var phraseElement = document.getElementById("phrase");
    phraseElement.innerHTML = phrase;
}
window.onload = makePhrases;
</script>
</head>
<body>
<h1>Phrase-o-Matic says:</h1>

<p id="phrase"></p>

</body>
</html>

This is an example from a book i am reading on javascript why does the: 这是我在javascript上阅读的一本书的例子为什么:

var rand1 = Math.floor(Math.random() * words1.length);
    var rand2 = Math.floor(Math.random() * words2.length);
    var rand3 = Math.floor(Math.random() * words3.length);

generate values that always round down to an index value in the arrays words1 , words2 and words3 ? 生成值总是向下舍入到数组words1words2words3的索引值? Why does it never get a value above the last index number of 4 为什么它永远不会得到高于最后一个索引号4的值

Math.random() * words1.length means [0, words1.length) . Math.random() * words1.length表示[0, words1.length) So the value always smaller than words1.length . 所以值总是小于words1.length

floor() rounds down (always down) to the nearest integer. floor()向下舍入(总是向下)到最接近的整数。 Math.random() generates a number greater than or equal to 0 and less than 1. Math.random()生成一个大于或等于0且小于1的数字。

If the length is 4, random() will generate a number between 0 and 3.9999999999, and round down. 如果长度为4,则random()将生成0到3.9999999999之间的数字,并向下舍入。 So the value will always be a valid index (0 to 3 inclusive) for a 4-element array. 因此,对于4元素数组,该值始终是有效索引(0到3,包括0和3)。

Math.random() returns a value between 0 (inclusive) and 1 (exclusive). Math.random()返回0(包括)和1(不包括)之间的值。 So you may get for instance 0.1234. 所以你可能会得到0.1234。 You then multiply this by the length of the array which is 4 in this case. 然后将此乘以数组的长度,在这种情况下为4。 The lowest possible value is 0 * 4 = 0 and the highest possible value is 0.9999 * 4 < 4. 最低可能值为0 * 4 = 0,最高可能值为0.9999 * 4 <4。

The floor function truncates the the number leaving only the integer part which will be in the range 0, 1, 2 or 3. floor函数截断数字,只留下整数部分,它将在0,1,2或3范围内。

Math.random() return a float value in [0, 1), which means : Math.random()在[0,1 ]中返回一个浮点值,表示:

from 0 (inclusive) up to but not including 1 (exclusive) 从0(含)到最高但不包括1(独家)

So Math.random() * 4 will be [0, 4), and Math.floor means: 因此Math.random()* 4将为[0,4],而Math.floor意味着:

the largest integer less than or equal to a number 小于或等于数字的最大整数

So Math.floor([0,3.xxxx...]) means [0,3] 所以Math.floor([0,3.xxxx ...])表示[0,3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM