[JS] JavaScript 數值與算數(Number and Math)
2 ** 3; // 2 的 3 次方(exponential)
num += 1; // 遞增
num -= 1; // 遞減
常用
產生隨機號碼
keywords: generate
, random number
, snippet
snippets: random
Math.random()
會產生 0(包含)到 1(不包含)的數值。
// 包含 min 和 max 的整數
function getRandomIntInclusive(min, max) {
// min = Math.ceil(min);
// max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; // The maximum is inclusive and the minimum is inclusive
}
- Math.random() @ MDN
- Generating Random Whole Numbers in JavaScript in a Specific Range @ StackOverflow
- Generate 4 digit random number using substring @ StackOverflow