Math任務(wù)
1邢滑、寫(xiě)一個(gè)函數(shù)腐螟,返回從min到max之間的 隨機(jī)整數(shù),包括min不包括max
function getRandom(min,max){
return Math.floor(Math.random() * (max-min)+min)
}
2困后、寫(xiě)一個(gè)函數(shù)遭垛,返回從min都max之間的 隨機(jī)整數(shù),包括min包括max
function getRandom(min,max){
return Math.floor(Math.random() * (max-min+1)+min)
}
3操灿、寫(xiě)一個(gè)函數(shù)锯仪,生成一個(gè)長(zhǎng)度為 n 的隨機(jī)字符串,字符串字符的取值范圍包括0到9趾盐,a到 z庶喜,A到Z。
function getRandom(len){
var str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var result = "";
for(var i = 0;i < 10;i++){
result += str[Math.floor(Math.random()*62)];
}
return result;
}
4救鲤、寫(xiě)一個(gè)函數(shù)久窟,生成一個(gè)隨機(jī) IP 地址,一個(gè)合法的 IP 地址為 0.0.0.0~255.255.255.255
function getRandIP(){
var ip = parseInt(Math.random()*256);
for (var i = 0 ; i < 3;i++){
ip = ip + "."+parseInt(Math.random()*256) ;
}
return ip ;
}
5本缠、寫(xiě)一個(gè)函數(shù)斥扛,生成一個(gè)隨機(jī)顏色字符串,合法的顏色為#000000~ #ffffff
function getRandColor() {
var dist = '0123456789abcdef'
var str = '',ch;
for(var i = 0; i<6; i++){
ch = dist[Math.floor(Math.random()*16)];
console.log(ch);
str += ch;
}
return '#'+str
}
數(shù)組任務(wù)
1丹锹、數(shù)組方法里push稀颁、pop、shift楣黍、unshift匾灶、join、splice分別是什么作用租漂?用 splice函數(shù)分別實(shí)現(xiàn)push阶女、pop、shift哩治、unshift方法
1.push方法可以接收任意數(shù)量的參數(shù)秃踩,把他們逐個(gè)添加到數(shù)組末尾,并返回修改后數(shù)組的長(zhǎng)度
2.pop()放法則從數(shù)組末尾一處最后一項(xiàng)业筏,減少數(shù)組的length值憔杨,然后返回一處的項(xiàng)
3.shift()方法:移除數(shù)組中的第一項(xiàng),并返回該項(xiàng)驾孔,同時(shí)數(shù)組的長(zhǎng)度減1
4.unshift()方法: 能在數(shù)組前端添加任意個(gè)項(xiàng)芍秆,并返回新數(shù)組的長(zhǎng)度
5.join() 方法則可以使用不同的分隔符來(lái)構(gòu)建一個(gè)字符串惯疙。 join()法只接收一個(gè)參數(shù),即用作分隔符的字符串妖啥,然后返回包含所有數(shù)組項(xiàng)的字符串
6.arr.splice(index,howmany,item): 從數(shù)組的第index個(gè)參數(shù)開(kāi)始霉颠,刪除howmany個(gè)參數(shù),并添加item為數(shù)組的參數(shù)
7.用splice實(shí)現(xiàn)push:arr.splice(arr.length,0,element)
8.用splice實(shí)現(xiàn)pop:arr.splice(arr.length-1,1)
9.用splice實(shí)現(xiàn)shift:arr.splice(0,1)
10.用splice實(shí)現(xiàn)shift:arr.splice(0,0,element)
2荆虱、寫(xiě)一個(gè)函數(shù)蒿偎,操作數(shù)組,數(shù)組中的每一項(xiàng)變?yōu)樵瓉?lái)的平方怀读,在原數(shù)組上操作
function squareArr(arr){
for(var i = 0;i < arr.length;i++){
arr[i] = arr[i]*arr[i];
}
return arr;
}
3诉位、寫(xiě)一個(gè)函數(shù),操作數(shù)組菜枷,返回一個(gè)新數(shù)組苍糠,新數(shù)組中只包含正數(shù),原數(shù)組不變
function filterPositive(arr){
var newArr = []
newArr = arr.filter(function(e) {
return e > 0 && typeof e === "number";
})
return newArr
}
Date 任務(wù)
1啤誊、 寫(xiě)一個(gè)函數(shù)getChIntv岳瞭,獲取從當(dāng)前時(shí)間到指定日期的間隔時(shí)間
function getChIntv(strDate){
var strIntv = "";
var oneDay = 24 * 60 * 60 *1000;
var oneHour = 60 * 60 *1000;
var oneMin = 60 *1000;
var secondIntv = (Date.parse(strDate) - 8 * oneHour - Date.now());
var day = parseInt(secondIntv / oneDay);
var hour = parseInt((secondIntv % (oneDay)) / oneHour);
var min = parseInt(((secondIntv % (oneDay)) % oneHour) / oneMin);
var second = ((secondIntv % (oneDay)) % oneHour) % oneMin;
var strIntv = "距" + strDate + "還有" + day + "天" + hour + "小時(shí)" + min + "分" + second + "秒"
return strIntv
}
var str = getChIntv('2017-10-01');
console.log(str);
2、把hh-mm-dd格式數(shù)字日期改成中文日期
function getChsDate(strDate){
var dict = ["零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一"];
var arrDate = strDate.split("-");
var strYear = "";
var strMonth = "";
var strDay = "";
var strChsDate = "";
for(var i = 0; i < arrDate.length; i++){
arrDate[i] = Number(arrDate[i])
}
for(var j = 0; j < 4; j++){
strYear += dict[(arrDate[0]).toString()[j]];
}
strMonth = dict[arrDate[1]];
strDay = dict[arrDate[2]];
strChsDate = strYear + "年" + strMonth + "月" + strDay + "日";
return strChsDate;
}
var str = getChsDate("2017-10-01");
console.log(str);
3蚊锹、寫(xiě)一個(gè)函數(shù)瞳筏,參數(shù)為時(shí)間對(duì)象毫秒數(shù)的字符串格式,返回值為字符串牡昆。假設(shè)參數(shù)為時(shí)間對(duì)象毫秒數(shù)t姚炕,根據(jù)t的時(shí)間分別返回如下字符串:
- 剛剛( t 距當(dāng)前時(shí)間不到1分鐘時(shí)間間隔)
- 3分鐘前 (t距當(dāng)前時(shí)間大于等于1分鐘,小于1小時(shí))
- 8小時(shí)前 (t 距離當(dāng)前時(shí)間大于等于1小時(shí)丢烘,小于24小時(shí))
- 3天前 (t 距離當(dāng)前時(shí)間大于等于24小時(shí)柱宦,小于30天)
- 2個(gè)月前 (t 距離當(dāng)前時(shí)間大于等于30天小于12個(gè)月)
- 8年前 (t 距離當(dāng)前時(shí)間大于等于12個(gè)月)
function friendlyDate(time){
var nowTime = Date.now() - parseInt(time);
if(nowTime < 60000){
return "剛剛";
}
else if(nowTime < 3600000){
return (parseInt(nowTime / 60000) + "分鐘前");
}
else if(nowTime < 86400000){
return (parseInt(nowTime / 3600000) + "小時(shí)前");
}
else if(nowTime < 2592000000){
return (parseInt(nowTime / 86400000) + "天前");
}
else if(nowTime < 31104000000){
return (parseInt(nowTime / 2592000000) + "個(gè)月前");
}
else{
return (parseInt(nowTime / 31104000000) + "年前");
}
}
var str = friendlyDate('1484286674522');
console.log(str);