Math任務
1、寫一個函數,返回從min到max之間的 隨機整數,包括min不包括max
function getRandom(min,max){
return Math.floor(Math.random()*(max-min))+min;
}
console.log(getRandom(0,10))
2、寫一個函數,返回從min都max之間的 隨機整數,包括min包括max
function getRandom(min,max){
return Math.floor(Math.random()*(max-min+1))+min;
}
console.log(getRandom(0,10))
3、寫一個函數,生成一個長度為 n 的隨機字符串,字符串字符的取值范圍包括0到9,a到 z,A到Z
function getRandStr(len){
var dict = '123456789qwertyuioplkjhgfdsazxcvbnmQAZXSWEDCVFRTGBNHYUJMKIOLP';
var str = "";
for(var i=0; i<len; i++){
str += dict[Math.floor(Math.random()*dict.length)];
}
return str;
}
console.log (getRandStr(10));
4、寫一個函數,生成一個隨機 IP 地址,一個合法的 IP 地址為 0.0.0.0~255.255.255.255
function getRandIP(){
var arr = [];
for(var i = 0; i<4; i++){
arr.push(Math.floor(Math.random()*255));
}
return arr.join(".");
}
var ip = getRandIP()
console.log(ip)
5、寫一個函數,生成一個隨機顏色字符串,合法的顏色為#000000~ #ffffff
function getRandColor(){
var dict = "1234567890abcdef";
var str = "";
for(var i = 0; i<6; i++){
str += dict[Math.floor(Math.random()*dict.length)];
}
return "#"+str;
}
var color = getRandColor()
console.log(color)
數組任務
1.數組方法里push、pop、shift、unshift、join、splice分別是什么作用?用 splice函數分別實現push、pop、shift、unshift方法
push:在數組的末尾添加一個或者多個元素,并返回新的長度;
pop:刪除數組的最后一個元素,并返回該元素的值;
shift:刪除數組的第一個元素,并返回該元素的值;
unshift:將一個或多個元素添加到數組的開頭,并返回新數組的長度;
join:將數組的(或者一個類數組對象)的所用元素鏈接到一個字符串中;
splice:通過刪除現有元素和/或添加新元素來更改一個數組的內容;
var arr = [0,1,2,3]
arr.splice(arr.length,0,4)//push
console.log(arr)//[0,1,2,3,4]
arr.splice(arr.length,1)//pop
console.log(arr)//[0,1,2,]
arr.splice(0,1)//shift
console.log(arr)//[1, 2, 3]
arr.splice(0,0,0)//unshift
console.log(arr)//[0, 0, 1, 2, 3]
2、寫一個函數,操作數組,數組中的每一項變為原來的平方,在原數組上操作
function squareArr(arr){
for(var i in arr){
arr[i] =Math.pow(arr[i],2);
}
return arr;
}
var arr = [2, 4, 6]
squareArr(arr)
console.log(arr) // [4, 16, 36]
3、寫一個函數,操作數組,返回一個新數組,新數組中只包含正數,原數組不變
function filterPositive(arr){
var newArr = [];
for(var i=0; i<arr.length; i++){
if(typeof arr[i]==="number"&&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、 寫一個函數getChIntv,獲取從當前時間到指定日期的間隔時間
function getChIntv(dateStr){
var targetDate = new Date(dateStr)
var curDate = new Date()
var offset = Math.abs(targetDate-curDate)
var totalSeconds = Math.floor(offset/1000)
var second = totalSeconds%60
var totalMinutes = Math.floor((offset/1000)/60)
var minutes = totalMinutes%60
var totalHours = Math.floor(totalMinutes/60)
var hours = totalHours%24
var totalDays = Math.floor(totalHours/24)
return totalDays + "天" + hours + "小時" + minutes + "分鐘" + second + "秒"
}
var str = getChIntv("2017-02-08");
console.log(str);
2、把hh-mm-dd格式數字日期改成中文日期
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、寫一個函數,參數為時間對象毫秒數的字符串格式,返回值為字符串。假設參數為時間對象毫秒數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 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' ) // 1分鐘前
var str2 = friendlyDate('1483941245793') //4天前
【個人總結,如有錯漏,歡迎指出】
:>