Math對象不同於String、Date和Array對象。Math不是構造器也就是不是構造函數,在使用屬性和方法時,是Math直接使用,不需要對象實例化 。後三者都是構造器,使用它們的方法和屬性時需要 聲明相應的變量。
數學對象: Math.IP
基本字符串:var str = 'asdf'; str.lenght
日期對象:var date = new Date();
數組對象:var arr = [1,2,3,4] 等價於<====> var arr = new Array(1,2,3,4)
Math常用方法
Math.abs() 絕對值方法 取參數的絕對值
Math.ceil() 向上取整 取大值
Math.floor() 向下取整 取小值
//Math 不是一個構造函數 他的方法和屬性直接使用的
// Math 的使用
// PI是Math的屬性
console.log(Math.PI);
// Math.abs() 絕對值方法 取參數的絕對值
console.log(Math.abs(-2)); // 2
console.log(Math.abs(2)); // 2
// Math.ceil() 向上取整 取大值
console.log(Math.ceil(2.1)); // 3
console.log(Math.ceil(2.9)); // 3
console.log(Math.ceil()); // NaN
// Math.floor() 向下取整 取小值
console.log(Math.floor(2.1)); // 2
console.log(Math.floor(2.9)); // 2
// Math.max() 函數返回一組數中的最大值。 注意是數組
console.log(Math.max(2, 3, 44, 22)); // 44
console.log(Math.max(2, 3, 44, 'pink')); // NaN 如果數組元素中有不能轉換成數值的元素會返回NaN
console.log(Math.max()); // -Infinity 無參數返回-Infinity
// Math.min() 返回零個或更多個數值的最小值。
console.log(Math.min(2, 3, 44, 22)); // 2
console.log(Math.min(2, 3, 44, 'pink')); // 2
console.log(Math.min()); // Infinity
Math.round() 函數返回一個數字四捨五入後最接近的整數。 .5的取捨 誰大取誰
// Math.round() 函數返回一個數字四捨五入後最接近的整數。 .5的取捨 誰大取誰
console.log(Math.round(1.5)); // 2
console.log(Math.round(1.6)); // 2
console.log(Math.round(1.2)); // 1
console.log(Math.round(-1.5)); // -1 .5的取捨 誰大取誰. 此時的.5不入,-1 和 -2,-1大取-1
console.log(Math.round(-1.6)); // -2
console.log(Math.round(-1.2)); // -1
Math.max() 函數返回一組數中的最大值。 注意是數組
如果數組元素中有不能轉換成數值的元素會返回NaN
無參數返回-Infinity
Math.min() 返回零個或更多個數值的最小值。
如果數組元素中有不能轉換成數值的元素會返回 NaN
無參數返回 Infinity
Math.random() 函數返回一個浮點, 返回的數值在0-1之間,包括0但不包括1.
// Math.random() 函數返回一個浮點, 返回的數值在0-1之間,包括0但不包括1.
// 可以作爲隨機種子產生隨機數
console.log(Math.random()); // [0,1) 之間的數
// 兩個數之間的隨機數
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
console.log(getRandomArbitrary(2, 10));
// 兩個整數之間的隨機數,包含這倆數
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}
console.log(getRandomIntInclusive(2, 10));
random()方法的運用
猜數字的小遊戲
產生一個隨機數,讓用戶猜這個隨機數,同時提醒用戶猜大了還是猜小了。
// 兩個整數之間的隨機數,包含這倆數 次數不限
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}
var inputNum = prompt('歡迎來到猜字遊戲。請輸入一個1~10之間的數字:');
var randomNum = getRandomIntInclusive(1, 10);
while (true) {
if (inputNum > randomNum) {
alert('你猜大了');
} else if (inputNum < randomNum) {
alert('你猜小了');
} else {
alert('你真棒!猜對了!');
break;
}
}
升級版猜數字小遊戲
在上一個例子中,我們猜數字沒有次數要求,這次要求猜的次數,在有限的次數中猜數字
// 兩個整數之間的隨機數,包含這倆數 次數不限
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}
alert('歡迎來到猜字遊戲。你只有十次機會哦。');
var randomNum = getRandomIntInclusive(1, 100);
for (var i = 1; i <= 10; i++) {
var inputNum = prompt('請輸入一個1~100之間的數字:');
if (inputNum > randomNum) {
alert('你猜大了,你還有' + (10 - i) + '次機會');
} else if (inputNum < randomNum) {
alert('你猜小了,你還有' + (10 - i) + '次機會');
} else {
alert('你真棒!猜對了!');
break;
}
}
暫時結束了內置對象的學習了,大概過了一下這些對象常用的方法。
後續會繼續深入的。
加油加油加油!!!!!