Math數(shù)組Date

Math

1、寫一個(gè)函數(shù),返回從min到max之間的隨機(jī)整數(shù),包括min不包括max
  function random(a,b){
    return a+Math.floor(Math.random()*(b-a))
  }
2、寫一個(gè)函數(shù),返回從min都max之間的隨機(jī)整數(shù),包括min包括max
  function random(a,b){
    return a+Math.floor(Math.random()*(b-a+1))
  }
3、寫一個(gè)函數(shù),生成一個(gè)長(zhǎng)度為 n 的隨機(jī)字符串,字符串字符的取值范圍包括0到9,a到 z,A到Z。
  function random(a,b){
    return a+Math.floor(Math.random()*(b-a))
  }
  // 返回隨機(jī)字符串
  function getRandStr(len){
     var dict='1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
     var str = ''
      for(var i = 0 ; i < len ;i++){
        str = str +dict[random(0,62)]
      }
      return str;
   }
   var str = getRandStr(10);
   console.log(str)  //隨機(jī)輸出一個(gè)10位的字符串
4、寫一個(gè)函數(shù),生成一個(gè)隨機(jī) IP 地址,一個(gè)合法的 IP 地址為 0.0.0.0~255.255.255.255
  function random(a,b){
    return a+Math.floor(Math.random()*(b-a))
  }
  // 隨機(jī)獲取ip
  function getRandIP(){
    var arr = []    //聲明一個(gè)空數(shù)組
    for(var i =0;i<4;i++){
      arr.push(random(0,256))
    }
  return arr.join('.')    //每個(gè)數(shù)值中間添加'.'分割
  }
  var ip = getRandIP()
  console.log(ip) // 10.234.121.45
5、寫一個(gè)函數(shù),生成一個(gè)隨機(jī)顏色字符串,合法的顏色為#000000~ #ffffff
  function random(a,b){
    return a+Math.floor(Math.random()*(b-a))
  }
  function getRandColor(){
    var dict='0123456789ABCDEFabcdef'  //隨機(jī)庫
    var str = ''     
      for(var i = 0 ; i < 6 ;i++){
        str = str +dict[random(0,22)];
      }
        return '#' + str; 
      }
  var color = getRandColor()
  console.log(color)  //隨機(jī)輸出一個(gè)顏色

數(shù)組

1、數(shù)組方法里push、pop、shift、unshift、join、splice分別是什么作用?用 splice函數(shù)分別實(shí)現(xiàn)push、pop、shift、unshift方法
  • push,pop(棧方法)

我們很多時(shí)候希望刪除中間一個(gè)元素后,后面元素的index都自動(dòng)減一,數(shù)組length同時(shí)減一,就好像在一個(gè)堆棧中拿去的一個(gè),數(shù)組已經(jīng)幫我們做好了這種操作方式,poppush能夠讓我們使用堆棧那樣先入后出使用數(shù)組。
作用:push用來在數(shù)組末尾添加數(shù)值;pop用來刪除數(shù)組末尾一個(gè)數(shù)值

  var a = new Array(1,2,3);
  a.push(4);   //在末尾添加數(shù)值4
  console.log(a);//[1, 2, 3, 4]
  console.log(a.length);//4
  console.log(a.pop());//4
  console.log(a); //[1, 2, 3]
  console.log(a.length);//3
  • shift,unshift(隊(duì)列方法)

既然棧方法都實(shí)現(xiàn)了,先入先出的隊(duì)列怎么能少,shift方法可以刪除數(shù)組index最小元素,并使后面元素index都減一,length也減一,這樣使用shift/push就可以模擬隊(duì)列了,當(dāng)然與shift方法對(duì)應(yīng)的有一個(gè)unshift方法,用于向數(shù)組頭部添加一個(gè)元素
作用:unshift往數(shù)組的第一個(gè)位置添加一個(gè)數(shù)值;shift刪除數(shù)組的第一個(gè)數(shù)值

  var a=new Array(1,2,3);
  a.unshift(4);   //數(shù)組第一位添加數(shù)值4
  console.log(a);//[4, 1, 2, 3]
  console.log(a.length);//4    
  console.log(a.shift());//4    //刪除數(shù)組第一位
  console.log(a); //[1, 2, 3]
  console.log(a.length);//3
  • join

這個(gè)方法在C#等語言中也有,作用是把數(shù)組元素(對(duì)象調(diào)用其toString()方法)使用參數(shù)作為連接符連接成一字符串,不會(huì)修改原數(shù)組內(nèi)容

  var a = new Array(1,2,3,4,5);
  console.log(a.join(',')); //1,2,3,4,5
  console.log(a.join(' ')); //1 2 3 4 5
  • splice(終極神器)推薦指數(shù):★★★★★

JavaScript提供了一個(gè)splice方法用于一次性解決數(shù)組添加、刪除(這兩種方法一結(jié)合就可以達(dá)到替換效果),方法有三個(gè)參數(shù)

  • 1.開始索引
  • 2.刪除元素的位移
  • 3.插入的新元素,當(dāng)然也可以寫多個(gè)
    splice方法返回一個(gè)由刪除元素組成的新數(shù)組,沒有刪除則返回空數(shù)組
  var a = new Array(1,2,3,4,5);
刪除

指定前兩個(gè)參數(shù),可以使用splice刪除數(shù)組元素,同樣會(huì)帶來索引調(diào)整及l(fā)ength調(diào)整

  var a = new Array(1,2,3,4,5);
  console.log(a.splice(1,3));//[2, 3, 4]
  console.log(a.length);//2
  console.log(a);//[1,5]

如果數(shù)組索引不是從0開始的,那么結(jié)果會(huì)很有意思,有一這樣數(shù)組

  var a = new Array();
  a[2]=2;
  a[3]=3;
  a[7]=4;
  a[8]=5;
console.log(a.splice(3,4)); //[3]
console.log(a.length); //5
console.log(a); //[2: 2, 3: 4, 4: 5]

上面例子可以看到,splice的第一個(gè)參數(shù)是絕對(duì)索引值,而不是相對(duì)于數(shù)組索引,第二個(gè)參數(shù)并不是刪除元素的個(gè)數(shù),而是刪除動(dòng)作執(zhí)行多少次,并不是按數(shù)組實(shí)際索引移動(dòng),而是連續(xù)移動(dòng)。同時(shí)調(diào)整后面元素索引,前面索引不理會(huì)

插入與替換

只要方法第二個(gè)參數(shù),也就是刪除動(dòng)作執(zhí)行的次數(shù)設(shè)為0,第三個(gè)參數(shù)及以后填寫要插入內(nèi)容就splice就能執(zhí)行插入操作,而如果第二個(gè)參數(shù)不為0則變成了先在該位置刪除再插入,也就是替換效果

  var a = new Array(1,2,3,4,5);
  a.splice(1,0,9,99,999);
  console.log(a.length); //8
  console.log(a);//[1, 9, 99, 999, 2, 3, 4, 5]
  a.splice(1,3,8,88,888);
  console.log(a.length);//8
  console.log(a);//[1, 8, 88, 888, 2, 3, 4, 5]
  • 用 splice函數(shù)分別實(shí)現(xiàn)push、pop、shift、unshift方法
  var arr = [1,2,3,4,5,6,7]
  arr.splice(7,0,8)   //相當(dāng)于arr.push(8)在末尾添加數(shù)值8
  arr.splice(7,1)   //8  相當(dāng)于arr.pop()  刪除最后一個(gè)數(shù)值 
  arr.splice(0,0,1)   // 相當(dāng)于arr.unshift(0)  在數(shù)組首添加數(shù)值0 
  arr.splice(0,1)    // 相當(dāng)于arr.shift() 刪除數(shù)組首個(gè)數(shù)值
  

其他操作

  • slice(start,end)

不要和splice方法混淆,slice方法用于返回?cái)?shù)組中一個(gè)片段或子數(shù)組,如果只寫一個(gè)參數(shù)返回參數(shù)到數(shù)組結(jié)束部分,如果參數(shù)出現(xiàn)負(fù)數(shù),則從數(shù)組尾部計(jì)數(shù)(-3意思是數(shù)組倒第三個(gè),一般人不會(huì)這么干,但是在不知道數(shù)組長(zhǎng)度,想舍棄后n個(gè)的時(shí)候有些用,不過數(shù)組長(zhǎng)度很好知道。。。。,好糾結(jié)的用法),如果start大于end返回空數(shù)組,值得注意的一點(diǎn)是slice不會(huì)改變?cè)瓟?shù)組,而是返回一個(gè)新的數(shù)組
(通俗的來講,抽出一個(gè)數(shù)組,start是開始的數(shù)組位置,end是結(jié)束的數(shù)組位置,如果end為負(fù)就是倒著數(shù),start>end(除非為負(fù)值),不會(huì)影響原數(shù)組)

  var a = new Array(1,2,3,4,5);
  console.log(a); //[1, 2, 3, 4, 5]
  console.log(a.slice(1,2));//2
  console.log(a.slice(1,-1));//[2, 3, 4]
  console.log(a.slice(3,2));//[]
  console.log(a); //[1, 2, 3, 4, 5]
  • concat(array)

看起來像是剪切,但這個(gè)真不是形聲字,concat方法用于拼接數(shù)組,a.concat(b)返回一個(gè)a和b共同組成的新數(shù)組,同樣不會(huì)修改任何一個(gè)原始數(shù)組,也不會(huì)遞歸連接數(shù)組內(nèi)部數(shù)組

  var a = new Array(1,2,3,4,5);
  var b = new Array(6,7,8,9);
  console.log(a.concat(b));//[1, 2, 3, 4, 5, 6, 7, 8, 9]
  console.log(a); //[1, 2, 3, 4, 5]
  console.log(b); //[6, 7, 8, 9]
  • reverse()

方法用于將數(shù)組逆序,與之前不同的是它會(huì)修改原數(shù)組

  var a = new Array(1,2,3,4,5);
  a.reverse();
  console.log(a); //[5, 4, 3, 2, 1]

同樣,當(dāng)數(shù)組索引不是連續(xù)或以0開始,結(jié)果需要注意

  var a = new Array();
  a[2]=2;
  a[3]=3;
  a[7]=4;
  a[8]=5;
  a.reverse();
  • sort

sort方法用于對(duì)數(shù)組進(jìn)行排序,當(dāng)沒有參數(shù)的時(shí)候會(huì)按字母表升序排序,如果含有undefined會(huì)被排到最后面,對(duì)象元素則會(huì)調(diào)用其toString方法,如果想按照自己定義方式排序,可以傳一個(gè)排序方法進(jìn)去,很典型的策略模式,同樣sort會(huì)改變?cè)瓟?shù)組。

  var a=new Array(5,4,3,2,1);
  a.sort();
  console.log(a);//[1, 2, 3, 4, 5]

但是。。。

  var a=new Array(7,8,9,10,11);
  a.sort();
  console.log(a);//[10, 11, 7, 8, 9]

因?yàn)榘凑兆帜副砼判颍?就比10大了,這時(shí)候我們需要傳入自定義排序函數(shù)

  var a = new Array(7,8,9,10,11);

  a.sort(function(v1,v2){
      return v1-v2;
  });
  console.log(a);//[7, 8, 9, 10, 11]

sort內(nèi)部使用快速排序,每次比較兩個(gè)元素大小的時(shí)候如果沒有參數(shù),則直接判斷字母表,如果有參數(shù),則把正在比較的兩個(gè)參數(shù)傳入自定義方法并調(diào)用(正在比較的兩個(gè)數(shù)會(huì)傳給自定義方法的v1、v2),如果返回值大于0表示v1 > v2,如果等于0,表示v1 = v2,如果小于0,表示v1 < v2,其實(shí)我們傳入的方法就是告訴sort怎么比較兩個(gè)元素誰大誰小,至于排序移動(dòng)元素過程人家寫好了

2、寫一個(gè)函數(shù),操作數(shù)組,數(shù)組中的每一項(xiàng)變?yōu)樵瓉淼钠椒剑谠瓟?shù)組上操作
  function squareArr(arr){
    for(var i = 0 ; i<arr.length;i++){
      arr[i]=arr[i]*arr[i];
    }
    return arr
  }
  var arr = [2, 4, 6]
  squareArr(arr)
  console.log(arr) // [4, 16, 36]
3、寫一個(gè)函數(shù),操作數(shù)組,返回一個(gè)新數(shù)組,新數(shù)組中只包含正數(shù),原數(shù)組不變
  function filterPositive(arr){
    var newArr = [];
    for(var i = 0; i < arr.length; i++){
        if(arr[i] > 0 && arr[i] !== true){
            newArr.push(arr[i]);
        }
    }
    return nweArr;
    }
  var arr = [3, -1,  2,  '饑人谷', true]
  var newArr = filterPositive(arr)
  console.log(newArr) //[3, 2]
  console.log(arr) //[3, -1,  2,  '饑人谷', true]

Date

1、 寫一個(gè)函數(shù)getChIntv,獲取從當(dāng)前時(shí)間到指定日期的間隔時(shí)間
  function getChintv(dateStr){
    var targetDate = new Date(dateStr);    //獲取截止時(shí)間
    var presentDate = new Date();    //當(dāng)前時(shí)間設(shè)置
    var offset = Math.abs(targetDate-presentDate);    
    //目標(biāo)時(shí)間減去當(dāng)前時(shí)間

    var totalSeconds = Math.abs(offset/1000);  //總毫秒轉(zhuǎn)化為總秒數(shù)
    var second = Math.floor(totalSeconds%60);  //求余,得到秒數(shù)
    var totalMinutens = totalSeconds/60;    //總秒數(shù)轉(zhuǎn)為總分鐘
    var minuten = Math.floor(totalMinutens%60);  //求余,得到分鐘
    var totalHours = totalMinutens/60;  //總分鐘數(shù)轉(zhuǎn)為總小時(shí)數(shù)
    var hours = Math.floor(totalHours%60);  //求余,得到小時(shí)
    var totalDays = totalHours/60;  //總小時(shí)數(shù)轉(zhuǎn)為總天數(shù)
    var days = Math.floor(totalDays%24);  //求余,得到天數(shù)
    
    return days + '天' + hours + '小時(shí)' + minuten + '分' + second + '秒'
        }
  var d = getChintv('2017-10-8')
  console.log(d);  // 輸出時(shí)間
2、把hh-mm-dd格式數(shù)字日期改成中文日期
  function getChsDate(str){
    var dict = ["零","一","二","三","四","五","六","七","八","九","十","十一",
    "十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一",
    "二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十",
    "三十一"];  //漢字?jǐn)?shù)組
    var arr = str.split('-');    //去掉-,變?yōu)閿?shù)組
    var totalYear = arr[0];    
    var totalMonth = arr[1];
    var totalDay = arr[2];

    var year = dict[totalYear[0]] + dict[totalYear[1]] + dict[totalYear[2]] 
               + dict[totalYear[3]] + '年';
    var month = dict[totalMonth[0]] + dict[totalMonth[1]] + '月';
    var day = dict[totalDay[0]] + dict[totalDay[1]]+'日'

    return year + month + day;
  }

  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 offset = Date.now()-time;
      if(offset<(60*1000)){
        console.log("剛剛")
      }else if(offset<(60*60*1000)){
        console.log(Math.floor(offset/(60*1000))+'分鐘前')
      }else if(offset<(24*60*60*1000)){
        console.log(Math.floor(offset/(60*60*1000))+'小時(shí)前')
      }else if(offset<(30*24*60*60*1000)){
        console.log(Math.floor(offset/(24*60*60*1000))+'天前')
      }else if(offset<(12*30*24*60*60*1000)){
        console.log(Math.floor(offset/(30*24*60*60*1000))+'月前')
      }else{
        console.log(Math.floor(offset/(12*30*24*60*60*1000))+'年前')
      }
    }
  var str = friendlyDate( '1484286699422' ) //  6個(gè)月前
  var str2 = friendlyDate('1483941245793') //7個(gè)月前
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Math任務(wù) 1. 寫一個(gè)函數(shù),返回從min到max之間的隨機(jī)整數(shù),包括min不包括max 如下: 2. 寫一個(gè)函...
    _李祺閱讀 304評(píng)論 0 0
  • Math Math對(duì)象是JavaScript的內(nèi)置對(duì)象,提供一系列數(shù)學(xué)常數(shù)和數(shù)學(xué)方法。Math對(duì)象只提供了靜態(tài)的屬...
    yuhuan121閱讀 216評(píng)論 0 0
  • Math 1.寫一個(gè)函數(shù),返回從min到max之間的 隨機(jī)整數(shù),包括min不包括max 2.寫一個(gè)函數(shù),返回從mi...
    饑人谷_bigJiao閱讀 631評(píng)論 0 0
  • Math任務(wù) 1、寫一個(gè)函數(shù),返回從min到max之間的隨機(jī)整數(shù),包括min不包括max
    湖衣閱讀 348評(píng)論 0 0
  • 寫一個(gè)函數(shù),返回從min到max之間的 隨機(jī)整數(shù),包括min不包括max 寫一個(gè)函數(shù),返回從min都max之間的 ...
    LeeoZz閱讀 337評(píng)論 0 0

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