Math任務
1、寫一個函數(shù),返回從min到max之間的 隨機整數(shù),包括min不包括max
//這是一個0到10的隨機數(shù),不包括10
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min; }
getRandomInt(0, 10)
2、寫一個函數(shù),返回從min都max之間的 隨機整數(shù),包括min包括max
//這是一個0到10的隨機數(shù),包括0和10.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min+1)+min); }
getRandomInt(0, 10)
3、寫一個函數(shù),生成一個長度為 n 的隨機字符串,字符串字符的取值范圍包括0到9,a到 z,A到Z。
function getRandStr(numb){
var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
var getStr = "";
for(var i = 0;i <numb;i++){
var getNumb = Math.floor(Math.random()*62);
getStr = getStr + str[getNumb];
}
return getStr;
}
console.log(getRandStr(3));
4、寫一個函數(shù),生成一個隨機 IP 地址,一個合法的 IP 地址為 0.0.0.0~255.255.255.255
function getRandIP(){
var getStr = ' ';
for(var i = 0;i <4;i++){
var getNumb = Math.floor(1 + Math.random()*255);
getStr = getStr + getNumb + '.';
}
getStr = getStr.substr(0,getStr.length-1);
return getStr;
}
console.log(getRandIP());
5、寫一個函數(shù),生成一個隨機顏色字符串,合法的顏色為#000000~ #FFFFFF
function getRandColor(){
var str = '0123456789ABCDEF';
var color = ' ';
for(var i = 0;i < 6;i++){
var index = Math.floor(Math.random()*16);
color = color + str[index];
}
color = '#' + color;
return color;
}
var color = getRandColor(); console.log(color);
數(shù)組任務
1、數(shù)組方法里push、pop、shift、unshift、join、splice分別是什么作用?用splice 函數(shù)分別實現(xiàn)push、pop、shift、unshift方法
push方法用于在數(shù)組的末端添加一個或多個元素,并返回添加新元素后的數(shù)組長度。注意,該方法會改變原數(shù)組。
var a=[1,2,3,4,5];
a.push(1,2);//7
console.log(a);//[1, 2, 3, 4, 5, 1, 2]
pop方法用于刪除數(shù)組的最后一個元素,并返回該元素。注意,該方法會改變原數(shù)組。
var a=[1,2,4,5,6];
a.pop();//6
console.log(a)//[1, 2, 4,5]
shift方法用于刪除數(shù)組的第一個元素,并返回該元素。注意,該方法會改變原數(shù)組
var a=[1,2,4,5,6];
a.shift();//1
console.log(a);//[2,4,5,6]
concat方法用于多個數(shù)組的合并。它將新數(shù)組的成員,添加到原數(shù)組的尾部,然后返回一個新數(shù)組,原數(shù)組不變。
var a=[1,2,4,5,6];
a.concat(['jirengu','hangzhou']);//[2, 4, 5, 6, "jirengu", "hangzhou"]
console.log(a);//[1,2,4,5,6],原數(shù)組不變
join方法以參數(shù)作為分隔符,將所有數(shù)組成員組成一個字符串返回。如果不提供參數(shù),默認用逗號分隔。
var a=[1,2,4,5,6];
a.join(' $');//"1 $2 $4 $5 $6"
console.log(a);//[1,2,4,5,6]
unshift方法用于在數(shù)組的第一個位置添加元素,并返回添加新元素后的數(shù)組長度。注意,該方法會改變原數(shù)組。
var a=[1,2,4,5,6];
a.unshift('mhy');//6
console.log(a);//["mhy", 1, 2, 4, 5, 6]
splice方法用于刪除原數(shù)組的一部分成員,并可以在被刪除的位置添加入新的數(shù)組成員,返回值是被刪除的元素。注意,該方法會改變原數(shù)組。
var a=[1,2,4,5,6,4,8,34,235,2000];
a.splice(3,3,'mhy','jirengu');//[5, 6, 4],返回被刪除的部分
console.log(a);//[1, 2, 4, "mhy", "jirengu", 8, 34, 235, 2000],新的數(shù)組中含有新增加的元素
用splice 函數(shù)分別實現(xiàn)push、pop、shift、unshift 、splice方法
var a=['jirengu','hangzhou','mhy'];
//solice替代push給數(shù)組末尾添加元素
a.splice(3,0,'qingqing','ruoyu');//給a的末尾添加新元素
console.log(a);//["jirengu", "hangzhou", "mhy", "qingqing", "ruoyu"]
//solice替代pop刪除數(shù)組的最后一個元素
a.splice(4,1);//['ruoyu'],刪除末尾的元素
console.log(a);//["jirengu", "hangzhou", "mhy", "qingqing"]
//solice替代shift方法用于刪除數(shù)組的第一個元素
a.splice(0,1);//['jirengu'],刪除第一個的元素
console.log(a);//["hangzhou", "mhy", "qingqing"]
//solice替代unshift方法用于在數(shù)組的第一個位置添加元素
a.splice(0,0,'qingqing');
console.log(a.length);//4,新添加元素之后數(shù)組的長度
console.log(a);//["qingqing", "hangzhou", "mhy", "qingqing"]
//下面用splice函數(shù)實現(xiàn)pop和push共用屬性
var b=['good',2,3,4,5,666];
b.splice(1,4,"hello","world","beijng","pass");//[2, 3, 4, 5]
console.log(b);//["good", "hello", "world", "beijng", "pass", 666]
class MyArray extends Array {
fPush(...items) {
this.splice(this.length, 0, ...items);
}
fPop() {
this.splice(this.length - 1, 1);
}
fShift() {
this.splice(0, 1);
}
fUnshift(...items) {
this.splice(0, 0, ...items);
}
}
2、寫一個函數(shù),操作數(shù)組,數(shù)組中的每一項變?yōu)樵瓉淼钠椒剑谠瓟?shù)組上操作
function squareArr(arr){
for (var i=0; i<arr.length; i++) {
arr[i] = Math.pow(arr[i],2);
}
return arr;
}
var arr = [2, 4, 6];
squareArr(arr);
console.log(arr); // [4, 16, 36]
3、寫一個函數(shù),操作數(shù)組,返回一個新數(shù)組,新數(shù)組中只包含正數(shù),原數(shù)組不變
function filterPositive(arr){
var newArr = [];
for (var i =0; i<arr.length; i++) {
if (typeof arr[i] === 'number') {
if (arr[i] > 0) {
newArr.push(arr[i]);
}
}
}
return newArr;
}
var arr = [3, -1, 2, '饑人谷', true]
var newArr = filterPositive(arr)
console.log(newArr) //[3, 2]
console.log(arr) //[3, -1, 2, '饑人谷', true]
Date 任務
1、 寫一個函數(shù)getChIntv,獲取從當前時間到指定日期的間隔時間
function getChIntv(str) {
var nowTime = Date.now();
var endTime = Date.parse(str);
var gap = endTime - nowTime;
var days = parseInt(gap / (1000 * 60 * 60 * 24));
var hours = parseInt((gap % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = parseInt(((gap % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) / (1000 * 60));
var seconds = parseInt(((gap % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) % (1000 * 60) / 60);
return "距離除夕還有" + days + "天" + hours + "小時" + minutes + "分鐘" + seconds + "秒";
}
var str = getChIntv("2017-02-08");
console.log(str); // 距離除夕還有-163天-14小時-20分鐘-14秒
2、把hh-mm-dd格式數(shù)字日期改成中文日期
function getChsDate(str) {
var dist = ["零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一"];
var arr = str.split('-');
var year = arr[0];
var month = arr[1];
var day = arr[2];
var Chyear = dist[parseInt(year[0])] + dist[parseInt(year[1])] + dist[parseInt(year[2])] +dist[parseInt(year[3])] + '年';
var Chmonth = dist[parseInt(month)] + '月';
var Chday = dist[parseInt(day)] + '日';
return Chyear + Chmonth + Chday ;
}
var str = getChsDate('2015-01-08');
console.log(str); // 二零一五年一月八日
3、寫一個函數(shù),參數(shù)為時間對象毫秒數(shù)的字符串格式,返回值為字符串。假設參數(shù)為時間對象毫秒數(shù)t,根據(jù)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 now = Date.now();
var offset = (now - parseInt(time)) / 1000 / 60;
var result;
if ((offset / 60 / 24 / 30 / 12) >= 1 ) {
result = parseInt(offset / 60 / 24 / 30 / 12) + "年前";
}else if ((offset / 60 / 24 / 30) >= 1 ) {
result = parseInt(offset / 60 / 24 / 30) + "個月前"; }else if ((offset / 60 / 24 ) >=1 ) {
result = parseInt(offset / 60 / 24) + "天前";
}else if ((offset / 60 ) >=1) {
result = parseInt(offset / 60 ) + "小時前";
}else if (offset >=1) {
result = parseInt(offset) + "分鐘前";
}else if (offset <1) {
result = "剛剛";
} return result;
}
var str = friendlyDate( '1484286699422' );//"6個月前"
var str2 = friendlyDate('1483941245793');//"6個月前"