Math任務(wù)
1、寫一個(gè)函數(shù),返回從min到max之間的 隨機(jī)整數(shù),包括min不包括max
function roll(min,max){
return Math.floor(Math.random() * (max - min) + min)
}
2、寫一個(gè)函數(shù),返回從min都max之間的 隨機(jī)整數(shù),包括min包括max
function roll(min,max){
return Math.floor(Math.random() * (max - min + 1) + min)
}
3、寫一個(gè)函數(shù),生成一個(gè)長(zhǎng)度為 n 的隨機(jī)字符串,字符串字符的取值范圍包括0到9,a到 z,A到Z。
function getRandStr(len){
var dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",result = "";
for(var i = 0;i<len;i++){
result += dict[Math.floor(Math.random()*62)];
}
return result;
}
var str = getRandStr(10); // 0a3iJiRZap
4、寫一個(gè)函數(shù),生成一個(gè)隨機(jī) IP 地址,一個(gè)合法的 IP 地址為 0.0.0.0~255.255.255.255
function getRandIP(){
function randIP(){
return Math.floor(Math.random()*256);
}
return randIP()+"."+randIP()+"."+randIP()+"."+randIP();
}
var ip = getRandIP()
console.log(ip) // 10.234.121.45
5、寫一個(gè)函數(shù),生成一個(gè)隨機(jī)顏色字符串,合法的顏色為#000000~ #ffffff
function getRandColor(){
var dict = "0123456789abcdef",result = "";
for(var i = 0;i<6;i++){
result += dict[Math.floor(Math.random()*16)];
}
return "#"+result;
}
var color = getRandColor()
console.log(color) // #3e2f1b
數(shù)組任務(wù)
1、數(shù)組方法里push、pop、shift、unshift、join、splice分別是什么作用?用 splice函數(shù)分別實(shí)現(xiàn)push、pop、shift、unshift方法
- push(value):在數(shù)組未尾添加value元素,同時(shí)改變?cè)瓟?shù)組的index。
- pop():移除數(shù)組最后一個(gè)元素,同時(shí)改變?cè)瓟?shù)組的index。
- shift():移除數(shù)組第一個(gè)元素,同時(shí)改變?cè)瓟?shù)組的index。
- unshift(value):在數(shù)組頭部添加value元素,同時(shí)改變?cè)瓟?shù)組的index。
- join(char):將數(shù)組通過char連接符連接成一個(gè)字符串,不會(huì)修改原數(shù)組內(nèi)容。
- splice(index1,index2,value):用于一次性解決數(shù)組添加、刪除(這兩種方法一結(jié)合就可以達(dá)到替換效果),方法有三個(gè)參數(shù):index1.開始索引;index2.刪除元素的位移;value.插入的新元素,可以寫多個(gè),splice方法返回一個(gè)由刪除元素組成的新數(shù)組,不會(huì)修改原數(shù)組內(nèi)容。
arr.push(str)相等于arr.splice(arr.length,0,str)
arr.pop()相等于arr.splice(arr.length-1,1)
arr.shift()相等于arr.splice(0,1)
arr.unshift(str)相等于arr.splice(0,0,str)
2、寫一個(gè)函數(shù),操作數(shù)組,數(shù)組中的每一項(xiàng)變?yōu)樵瓉淼钠椒?,在原?shù)組上操作
function squareArr(arr){
arr.forEach(function(e,i,arr){
arr[i]= e * e;
});
}
var arr = [2, 4, 6];
squareArr(arr);
console.log(arr); // [4, 16, 36]
3、寫一個(gè)函數(shù),操作數(shù)組,返回一個(gè)新數(shù)組,新數(shù)組中只包含正數(shù),原數(shù)組不變
function filterPositive(a){
return a.filter(function(e){
return typeof e === "number"&& e>0;
});
}
var arr = [3, -1, 2, '饑人谷', true]
var newArr = filterPositive(arr)
console.log(newArr) //[3, 2]
console.log(arr) //[3, -1, 2, '饑人谷', true]
Date 任務(wù)
1、 寫一個(gè)函數(shù)getChIntv,獲取從當(dāng)前時(shí)間到指定日期的間隔時(shí)間
function getChIntv(string){
var targetDate = new Date(string),curDate = new Date();
var offset = Math.abs(targetDate - curDate);
var totalSecond = Math.floor(offset/1000);
var second = totalSecond%60;
var totalMinute = Math.floor(totalSecond/60);
var minute = totalMinute%60;
var totalHours = Math.floor(totalMinute/60);
var hours = totalHours%60;
var totalday = Math.floor(totalHours/24);
return "相距"+totalday+"天"+hours+"小時(shí)"+minute+"分鐘"+second+"秒";
}
var str = getChIntv("2017-06-07");
console.log(str);
2、把hh-mm-dd格式數(shù)字日期改成中文日期
function getChsDate(string){
var tempDate = new Date(string),yearStr = "",monthStr = "",dayStr = "";
var dict = "零一二三四五六七八九十";
var year = tempDate.getFullYear();
var month = tempDate.getMonth()+1;
var day = tempDate.getDate();
for(i=0;i<4;i++){
yearStr += dict[(year.toString()[i])];
}
if(month === 10){
monthStr = dict[10];
}
if(month>10){
monthStr = dict[10]+dict[month.toString()[1]];
}else{
monthStr = dict[month.toString()];
}
if(day === 10){
dayStr = dict[10];
}
if(day === day === 20 || day === 30){
dayStr = dict[day.toString[0]]+dict[10];
}
if(day<10){
dayStr = dict[day.toString()];
}
if(day>10 && day <20 && day !==20 && day !== 30){
dayStr = dict[10]+dict[day.toString()[1]];
}
if(day>20 && day <30 && day !==20 && day !== 30){
dayStr = dict[2]+dict[10]+dict[day.toString()[1]];
}
if(day>30){
dayStr = dict[3]+dict[10]+dict[day.toString()[1]];
}
return yearStr+"年"+monthStr+"月"+dayStr+"日";
}
var str = getChsDate('2015-01-08');
console.log(str); // 二零一五年一月八日
3、寫一個(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 targetDate = new Date(parseInt(time)),nowDate = new Date(),result = "";
var offset = nowDate - targetDate;
var totalMinute = Math.floor(Math.floor(offset/1000)/60);
if(totalMinute === 0){
result = "剛剛";
}
if(totalMinute > 0 && totalMinute < 60){
result = "3分鐘前";
}
if(totalMinute >59 && totalMinute < 1440){
result = "8小時(shí)前";
}
if(totalMinute > 1439 && totalMinute < 43200){
result = "3天前";
}
if(totalMinute > 43199 && totalMinute < 518400){
result = "2個(gè)月前";
}
if(totalMinute >518399){
result = "8年前";
}
return result;
}
var str = friendlyDate( '1484286699422' ) //Fri Jan 13 2017 13:51:39
var str2 = friendlyDate('1483941245793') //Mon Jan 09 2017 13:54:05