七漆撞、Math和Date
1.排序算法
sort()方法,用于對(duì)數(shù)組排序于宙。注意:該排序方法浮驳,是根據(jù)數(shù)組中,每一個(gè)元素首字符的unicode編碼進(jìn)行排序的
手寫(xiě)排序算法:
1.冒泡排序算法
2.選擇排序算法
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="UTF-8">
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>數(shù)組的排序方法</title>
</head>
<body>
? ? <script>
? ? ? ? // sort()方法捞魁,用于對(duì)數(shù)組排序
? ? ? ? let arr1 = [33,55,22,11,44]
? ? ? ? // console.log(arr1);
? ? ? ? // 注意:該排序方法至会,是根據(jù)數(shù)組中,每一個(gè)元素首字符的unicode編碼進(jìn)行排序的
? ? ? ? // arr1.sort()
? ? ? ? // 冒泡排序算法
? ? ? ? // 第一層循環(huán)谱俭,控制比較的輪數(shù) (數(shù)組長(zhǎng)度為5奉件,比較4輪)
? ? ? ? for(let i=0;i<arr1.length-1;i++){
? ? ? ? ? ? // 第二層循環(huán),控制每輪比較的次數(shù)(第一輪比較4次昆著,第四輪比較1次)
? ? ? ? ? ? for(let j=0;j<arr1.length-1-i;j++){
? ? ? ? ? ? ? ? // 每次用前一個(gè)數(shù) 去比較 后一個(gè)數(shù)
? ? ? ? ? ? ? ? if(arr1[j] > arr1[j+1]){
? ? ? ? ? ? ? ? ? ? //如果前一個(gè)數(shù) 大于 后一個(gè)數(shù) 就 互換位置
? ? ? ? ? ? ? ? ? ? //定義一個(gè)中間變量县貌,去換位置
? ? ? ? ? ? ? ? ? ? let temp = arr1[j]
? ? ? ? ? ? ? ? ? ? arr1[j] = arr1[j+1]
? ? ? ? ? ? ? ? ? ? arr1[j+1] = temp
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? console.log(arr1);
? ? ? ? // 選擇排序算法
? ? ? ? // 第一層循環(huán),控制每輪選擇的數(shù)
? ? ? ? for(let i=0;i<arr1.length-1;i++){
? ? ? ? ? ? // 第二層循環(huán)宣吱,控制每輪參與比較的數(shù)(第一輪窃这,j從1-4;第二輪征候,j從2-4)
? ? ? ? ? ? for(let j=i+1;j<arr1.length;j++){
? ? ? ? ? ? ? ? //如果選擇的數(shù) 大于 參與比較的數(shù) 就互換
? ? ? ? ? ? ? ? if(arr1[i] > arr1[j]){
? ? ? ? ? ? ? ? ? ? let temp = arr1[i]
? ? ? ? ? ? ? ? ? ? arr1[i] = arr1[j]
? ? ? ? ? ? ? ? ? ? arr1[j] = temp
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? console.log(arr1);
? ? </script>
</body>
</html>
控制臺(tái)顯示:
2.Math對(duì)象
Math對(duì)象 里面提供的方法杭攻,可以幫助我們解決算術(shù)問(wèn)題
提供的方法:
1.Math.random() 返回一個(gè)0到1之間的隨機(jī)數(shù)
2.abs() 返回一個(gè)數(shù)的絕對(duì)值
3.ceil() 向上取整
4.floor() 向下取整
5.max() 返回最大值
6.min() 返回最小值
7.pow() 返回指定數(shù)的次冪
8.round() 四舍五入
9.PI屬性,返回圓周率
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="UTF-8">
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Math對(duì)象</title>
</head>
<body>
? ? <script>
? ? ? ? // Math對(duì)象 里面提供的方法疤坝,可以幫助我們解決算術(shù)問(wèn)題
? ? ? ? // PI屬性兆解,返回的是圓周率
? ? ? ? console.log(Math.PI);
? ? ? ? // random() 返回一個(gè)0到1之間的隨機(jī)數(shù)
? ? ? ? console.log(Math.random());
? ? ? ? // 隨機(jī)返回一個(gè)1000-2000之間的數(shù)
? ? ? ? console.log(parseInt(Math.random()*1001)+1000);
? ? ? ? // abs() 返回一個(gè)數(shù)的絕對(duì)值
? ? ? ? console.log(Math.abs(-55));
? ? ? ? // ceil() 向上取整(只要有小數(shù),就進(jìn)一)
? ? ? ? console.log(Math.ceil(55.01));
? ? ? ? // floor() 向下取整(去掉所有小數(shù)點(diǎn))
? ? ? ? console.log(Math.floor(55.99))
? ? ? ? // max() 返回最大值
? ? ? ? console.log(Math.max(11,22,33,38,2,3,4));
? ? ? ? // min() 返回最小值
? ? ? ? console.log(Math.min(11,22,33,38,2,3,4));
? ? ? ? // pow() 返回指定數(shù)的次冪
? ? ? ? console.log(Math.pow(3,3));
? ? ? ? console.log(Math.pow(4,4));
? ? ? ? // round() 四舍五入
? ? ? ? console.log(Math.round(55.5));
? ? ? ? console.log(Math.round(55.4));
? ? ? ? console.log(Math.round(-55.5));
? ? ? ? console.log(Math.round(-55.6));
? ? ? ? // sqrt() 開(kāi)平方根
? ? ? ? console.log(Math.sqrt(16));
? ? </script>
</body>
</html>
控制臺(tái)顯示:
3.Date對(duì)象
創(chuàng)建并返回系統(tǒng)當(dāng)前日期
let date1 = new Date()
在創(chuàng)建日期對(duì)象時(shí)跑揉,可以傳遞一個(gè)時(shí)間戳參數(shù) 锅睛。時(shí)間戳:是從1970-1-1開(kāi)始的毫秒數(shù)
let date2 = new Date(123456789)
也可以根據(jù)一個(gè)指定的時(shí)間埠巨,返回一個(gè)日期對(duì)象
let date3 = new Date('2011-1-1 12:12:12')
提供的方法:
1.getFullYear() 返回年份
2.getMonth() 返回月份 返回的值是0-11(0表示1月份,11表示12月份)
3.getDate() 返回月份的日期
4.getDay() 返回星期幾 返回的值是0-6现拒,(0表示星期天)
5.getHours() 返回小時(shí) 返回的值是0-23(0表示凌晨12點(diǎn))
6.getMinutes() 返回分鐘
7.getSeconds() 返回秒
8.getMilliseconds() 返回毫秒
9.getTime() 返回時(shí)間戳
getXXX方法用于獲取時(shí)間對(duì)象中指定的部分
setXXX方法用于設(shè)置時(shí)間對(duì)象中指定的部分
練習(xí)題:
1.計(jì)算兩個(gè)日期相差多少天辣垒,注意:兩日期對(duì)象相減,返回的是兩個(gè)日期時(shí)間戳相減后的值
2.時(shí)間效果
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="UTF-8">
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Date對(duì)象</title>
</head>
<body>
? ? <script>
? ? ? ? // 創(chuàng)建并返回系統(tǒng)當(dāng)前日期
? ? ? ? let date1 = new Date()
? ? ? ? console.log(date1);
? ? ? ? // 也可以根據(jù)一個(gè)指定的時(shí)間印蔬,返回一個(gè)日期對(duì)象
? ? ? ? let date2 = new Date('2020-11-11 3:3:3')
? ? ? ? console.log(date2);
? ? ? ? // 在創(chuàng)建日期對(duì)象時(shí)勋桶,可以傳遞一個(gè)時(shí)間戳參數(shù)
? ? ? ? // 時(shí)間戳:是從1970-1-1開(kāi)始的毫秒數(shù)
? ? ? ? let date3 = new Date(1234234234232)
? ? ? ? console.log(date3);
? ? ? ? console.log('------------------------------');
? ? ? ? // getFullYear() 返回年份
? ? ? ? let year = date1.getFullYear()
? ? ? ? console.log(year);
? ? ? ? // getMonth() 返回月份 返回的值是0-11(0表示1月份,11表示12月份)
? ? ? ? let month = date1.getMonth()+1
? ? ? ? console.log(month);
? ? ? ? // getDate() 返回月份的日期
? ? ? ? let date = date1.getDate()
? ? ? ? console.log(date);
? ? ? ? console.log(`${year}-${month}-${date}`);
? ? ? ? // getHours() 返回小時(shí) 返回的值是0-23(0表示凌晨12點(diǎn))
? ? ? ? let hour = date1.getHours()
? ? ? ? console.log(hour);
? ? ? ? // getMinutes() 返回分鐘
? ? ? ? let minute = date1.getMinutes()
? ? ? ? console.log(minute);
? ? ? ? // getSeconds() 返回秒
? ? ? ? let second = date1.getSeconds()
? ? ? ? console.log(second);
? ? ? ? console.log(`${year}-${month}-${date} ${hour}:${minute}:${second}`);
? ? ? ? // getMilliseconds() 返回毫秒
? ? ? ? let millisecond = date1.getMilliseconds()
? ? ? ? console.log(millisecond);
? ? ? ? // getDay() 返回星期幾 返回的值是0-6侥猬,(0表示星期天)
? ? ? ? let day = date1.getDay()
? ? ? ? console.log(day);
? ? ? ? console.log('------------------------------');
? ? ? ? // 時(shí)間對(duì)象的方法getXXX()用于獲取指定的時(shí)間部分多律,setXXX()用于設(shè)置時(shí)間的指定部分
? ? ? ? // 每一個(gè)get方法都有一個(gè)對(duì)應(yīng)的set方法蔑赘。
? ? ? ? date1.setMonth(9)
? ? ? ? date1.setFullYear(2020)
? ? ? ? console.log(date1);
? ? ? ? console.log('------------------------------');
? ? ? ? // 兩個(gè)日期對(duì)象,可以加減運(yùn)算,返回的是兩個(gè)日期對(duì)象時(shí)間戳加減后的結(jié)果
? ? ? ? let d1 = new Date('2002-10-14')
? ? ? ? let d2 = new Date()
? ? ? ? let time = d2-d1? //兩個(gè)日期相減后的毫秒數(shù)
? ? ? ? console.log(time);
? ? ? ? console.log(Math.floor(time/1000));
? ? ? ? console.log(Math.floor(time/1000/60));
? ? ? ? console.log(Math.floor(time/1000/60/60));
? ? ? ? console.log(Math.floor(time/1000/60/60/24));
? ? </script>
</body>
</html>
控制臺(tái)顯示:
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>時(shí)間效果</title>
? ? <style>
? ? ? ? div {
? ? ? ? ? ? font-size: 30px;
? ? ? ? ? ? background-color: #369;
? ? ? ? ? ? color: white;
? ? ? ? ? ? display: inline-block;
? ? ? ? ? ? padding: 2px 10px;
? ? ? ? }
? ? </style>
</head>
<body>
? ? <div id="date">
? ? </div>
? ? <script>
? ? ? ? // 獲取網(wǎng)頁(yè)中的div元素(根據(jù)元素的id選擇器拓巧,獲取元素)
? ? ? ? let div = document.getElementById('date')
? ? ? ? //定時(shí)器劳较,讓定時(shí)器1000毫秒執(zhí)行一次
? ? ? ? setInterval(() => {
? ? ? ? ? ? // 創(chuàng)建一個(gè)當(dāng)前日期對(duì)象
? ? ? ? ? ? let now_date = new Date()
? ? ? ? ? ? // 獲取年月日谱净,時(shí)分秒
? ? ? ? ? ? let year = now_date.getFullYear()
? ? ? ? ? ? let month = now_date.getMonth() + 1
? ? ? ? ? ? let date = now_date.getDate()
? ? ? ? ? ? let hour = now_date.getHours()
? ? ? ? ? ? let minute = now_date.getMinutes()
? ? ? ? ? ? let second = now_date.getSeconds()
? ? ? ? ? ? //將獲取到的時(shí)間信息榨惰,拼接成一個(gè)字符串
? ? ? ? ? ? let str = [year, month, date].join('-') + ' ' + [hour, minute, second].join(':')
? ? ? ? ? ? //將拼接后的時(shí)間數(shù)據(jù),顯示在div中
? ? ? ? ? ? div.innerText = str
? ? ? ? }, 1000);
? ? </script>
</body>
</html>