Math任務
1、寫一個函數,返回從min到max之間的 隨機整數,包括min不包括max
function roll(min,max){
return Math.floor(Math.random() * (max - min) + min)
}
2、寫一個函數,返回從min都max之間的 隨機整數,包括min包括max
function roll(min,max){
return Math.floor(Math.random() * (max - min + 1) + min)
}
3、寫一個函數,生成一個長度為 n 的隨機字符串,字符串字符的取值范圍包括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、寫一個函數,生成一個隨機 IP 地址,一個合法的 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、寫一個函數,生成一個隨機顏色字符串,合法的顏色為#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
數組任務
1、數組方法里push、pop、shift、unshift、join、splice分別是什么作用?用 splice函數分別實現push、pop、shift、unshift方法
- push(value):在數組未尾添加value元素,同時改變原數組的index。
- pop():移除數組最后一個元素,同時改變原數組的index。
- shift():移除數組第一個元素,同時改變原數組的index。
- unshift(value):在數組頭部添加value元素,同時改變原數組的index。
- join(char):將數組通過char連接符連接成一個字符串,不會修改原數組內容。
- splice(index1,index2,value):用于一次性解決數組添加、刪除(這兩種方法一結合就可以達到替換效果),方法有三個參數:index1.開始索引;index2.刪除元素的位移;value.插入的新元素,可以寫多個,splice方法返回一個由刪除元素組成的新數組,不會修改原數組內容。
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、寫一個函數,操作數組,數組中的每一項變為原來的平方,在原數組上操作
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、寫一個函數,操作數組,返回一個新數組,新數組中只包含正數,原數組不變
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 任務
1、 寫一個函數getChIntv,獲取從當前時間到指定日期的間隔時間
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+"小時"+minute+"分鐘"+second+"秒";
}
var str = getChIntv("2017-06-07");
console.log(str);
2、把hh-mm-dd格式數字日期改成中文日期
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、寫一個函數,參數為時間對象毫秒數的字符串格式,返回值為字符串。假設參數為時間對象毫秒數t,根據t的時間分別返回如下字符串:
剛剛( t 距當前時間不到1分鐘時間間隔)
3分鐘前 (t距當前時間大于等于1分鐘,小于1小時)
8小時前 (t 距離當前時間大于等于1小時,小于24小時)
3天前 (t 距離當前時間大于等于24小時,小于30天)
2個月前 (t 距離當前時間大于等于30天小于12個月)
8年前 (t 距離當前時間大于等于12個月)
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小時前";
}
if(totalMinute > 1439 && totalMinute < 43200){
result = "3天前";
}
if(totalMinute > 43199 && totalMinute < 518400){
result = "2個月前";
}
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