1. Math.abs(target)
- 返回 target 絕對值
- 參數(shù):target: number
2. Math.ceil(target)
- 向上取整
- 參數(shù):target: number
3. Math.floor(target)
- 向下取整
- 參數(shù):target: number
4. Math.random()
- 返回一個浮點數(shù)腮猖,范圍[0,1)
取[10, 20)之間的隨機(jī)數(shù)
function random(min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min)) + min
}
取[10, 20]之間的隨機(jī)數(shù)
function random(min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min) + 1) + min
}
5. Math.round(target)
- 返回一個四舍五入的整數(shù)
要求:保留小數(shù)點后 n 位
function round(target, number) {
return Math.round(`${target}e${number}`) / Math.pow(10, number)
}
6. Math.min()
- 函數(shù)返回一組數(shù)中的最小值熄求。
- 參數(shù):number...
- 返回值:最小值|NaN
- 注意:無參數(shù)時,返回 Infinity
{
console.log(Math.min()) // Infinity
console.log(Math.min(1)) // 1
console.log(Math.min(1, 2)) // 1
console.log(Math.min(undefined)) // NaN
console.log(Math.min(true, false)) // 0
console.log(Math.min(1, '123')) // 123
console.log(Math.min(1, '123abc')) // NaN
console.log(Math.min(...[1, 2, 3, 4, 5, 6])) // 1
}
7. Math.max()
- 函數(shù)返回一組數(shù)中的最大值找岖。
- 參數(shù):number...
- 返回值:最大值|NaN
- 注意:無參數(shù)時圾笨,返回-Infinity
8. Math.PI
- 表示一個圓的周長與直徑的比例岭洲,約為 3.14159
9. Math.pow(base: number, exponent:number)
- 函數(shù)返回基數(shù)(base)的指數(shù)(exponent)次冪遵蚜,即 baseex^ponent苦始。
- 參數(shù):(base:number寞钥,exponent:number)
- 返回:number | NaN
- 注意:參數(shù)如果沒有或只有一個,則返回 NaN