進階任務(wù)6

Math任務(wù)

function getRandStr(min,max){
   return min+Math.floor(Math.random()*(max-min))
}
function getRandStr(min,max){
   return min+Math.floor(Math.random()*(max-min+1))
}
function random(a,b){
    return a+Math.floor(Math.random()*(b-a))
}

function getRandStr(n){
    var dict="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    var str=''
    for(i=0;i<n;i++){
        str+=dict[random(0,62)]
    }
    return str
}
function random(a,b){
    return a+Math.floor(Math.random()*(b-a))
}

function getRandIP(){
    var arr=[]
    for(i=0;i<4;i++){
       arr.push(random(0,256))
    }
    return arr.join('.')
}
var ip = getRandIP()
console.log(ip) 
function random(a,b){
    return a+Math.floor(Math.random()*(b-a))
}

function getRandColor(){
    var dict="0123456789abcdef"
    var str="#"
    for(i=0;i<6;i++){
        str+=dict[random(0,16)]
    }
    return str
}
var color = getRandColor()
console.log(color)

數(shù)組任務(wù)

push在數(shù)組最后添加一個元素
pop把數(shù)組最后一個元素拿出來,原數(shù)組改變
shift把數(shù)組第一位拿出來,原數(shù)組改變
unshiift在數(shù)組第一位新增元素
join把數(shù)組連接成字符串
splice在數(shù)組里的任意位置添加刪除元素

var arr=[1,2,3]
arr.splice(3,0,4)
console.log(arr) //[ 1, 2, 3, 4 ]
arr.splice(3,1)
console.log(arr) //[ 1, 2, 3 ]
arr.splice(0,0,'hello')
console.log(arr) //[ 'hello', 1, 2, 3 ]
arr.splice(0,1)
console.log(arr) //[ 1, 2, 3 ]
function squareArr(arr){
    for(i=0;i<arr.length;i++){
        arr[i]=arr[i]*arr[i]
    }
    return arr
}
var arr = [2, 4, 6]
squareArr(arr)
console.log(arr)
function filterPositive(arr){
    var newArr=[]
    for(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) 
console.log(arr) 

Date任務(wù)

var str = getChIntv("2018-02-15");
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 seconds=totalSeconds%60;
    var totalMinutes=Math.floor(totalSeconds/60)
    var mintues=totalMinutes%60
    var totalHours=Math.floor(totalMinutes/60)
    var hours=totalHours%24
    var totalDays=Math.floor(totalHours/24)
    return '距離除夕還有'+totalDays+'天'+hours+'小時'+mintues+'分'+seconds+'秒'
}
console.log(str); 
var str = getChsDate('2019-11-31');
function getChsDate(str) {
    var dict=["零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一"]
    var arr=str.split('-')
    var year=arr[0]
    var month=arr[1]
    var day=arr[2]
    var newyeararr=[]
    for(i=0,j=0;i<year.length,j<year.length;i++,j++){
        newyeararr[j]=dict[year[i]];
    }
    var newyearstr=newyeararr.join('')
    var newmonth=dict[Math.floor(month)]
    var newday=dict[Math.floor(day)]
    return newyearstr+'年'+newmonth+'月'+newday+'日'
}
console.log(str);
function friendlyDate(time){
    var now=Date.now()
    var offSetSeconds=(now-time)/1000
    if (offSetSeconds<60){
        return '剛剛'
    }else if(offSetSeconds<60*60){
        return'3分鐘前'
    }else if(offSetSeconds<60*60*24){
        return'8小時前'
    }else if(offSetSeconds<60*60*24*30){
        return'3天前'
    }else if(offSetSeconds<60*60*24*30*12){
        return'2個月前'
    }else {
        return'8年前'
    }
}
var str = friendlyDate( '1484286699422' ) //  1分鐘前
var str2 = friendlyDate('1183941245793') //4天前
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容