<!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>
這是一本書,我對JavaScript的閱讀爲例爲什麼用:爲什麼這個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);
生成始終向下舍到的指數值在陣列words1
,words2
和words3
值?爲什麼它從未得到高於最後索引號的值4
Protip:'~~'='Math.floor()' –
他們曾經說過'''over'OR' – Sir
Reading Head First HTML5,不是嗎? – MultiplyByZer0