美文网首页
Math数组Date

Math数组Date

作者: 饥人谷_啦啦啦 | 来源:发表于2017-06-20 03:11 被阅读0次

    1、写一个函数,返回从min到max之间的 随机整数,包括min不包括max

    function minMax(min,max){
      return Math.floor(Math.random()*(max-min)+min)
    }
    var a=minMax(50,100)
    console.log(a)
    

    2.写一个函数,返回从min都max之间的 随机整数,包括min包括max

    function minMax(min,max){
      return Math.floor(Math.random()*(max-min+1)+min)
    }
    var a=minMax(20,30)
    console.log(a)
    

    3.写一个函数,生成一个长度为 n 的随机字符串,字符串字符的取值范围包括0到9,a到 z,A到Z。

    var dict=[0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    function rd(n){
      var arr=[]
      arr.length=n
      for(i=0;i<arr.length;i++){
        arr[i]=dict[Math.floor(Math.random()*dict.length)]
      }
      return arr.join('')
    }
    var str1=rd(29)
    console.log(str1)
    

    4 、写一个函数,生成一个随机 IP 地址,一个合法的 IP 地址为 0.0.0.0~255.255.255.255

    function rdIp(){
      arr=new Array(4)
      for(var i=0;i<arr.length;i++){
        arr[i]=Math.floor(Math.random()*256)
      }
      return arr.join('.')
    }
    var a=rdIp()
    console.log(a)
    

    5、写一个函数,生成一个随机颜色字符串,合法的颜色为#000000~ #ffffff

    function rdColor(){
      var dict=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'],color
      arr=new Array(6)
      for(var i=0;i<arr.length;i++){
        arr[i]=dict[Math.floor(Math.random()*dict.length)]
      }
      return color='#'+arr.join('')
    }
    console.log(rdColor())
    

    1.数组方法里push、pop、shift、unshift、join、splice分别是什么作用?用 splice函数分别实现push、pop、shift、unshift方法。

    • push
      往数组最后面增添一个元素,会改变原数组 arr.push(要增加的元素);//返回增加的值

    • pop
      删除数组最后面的一个元素,改变原数组,arr.pop();//返回删除的值

    • shift
      删除数组最前面的一个元素,改变数组,arr.shift();//返回删除的值

    • unshift
      往数组最前面增添一个元素,改变原数组,arr.unshift(要增加的元素);//返回增加的值

    • join
      把数组中所有元素放入一个字符串,并且,可以指定分割符。
      arr.join('-')//返回字符串

    • splice
      splice 中文:拼接,结合。 在js中可以操作数组,增加/删除。
      用法:splice(index,howmany,item1,item2...)//返回删除的新数组
      其中index,起始位置,支持负数,负数就是倒序(负数时,第二个参数没用,只删除一次),(必选参数)
      howmany,删除多少次。(必选参数)
      items ,要增加的项目。(可选),改变原数组。

      • 容易和slice,split搞混(因为拼写有点像啊)
      • slice 切片,字符串和数组都可以用,a.slice(x,y)代表切片起始位置x和终止位置,支持负数。返回的是切片的字符串或者数组。
      • split,将字符串转换为数组,a.split(x,y) x选择字符串或正则表达式来作为分割线,并且会去掉他们,y,可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
    • 用splice实现push,pop,shift,unshift

    var a=[1,2,3,4,5],b
    a.push(6)
    a.splice(a.length,0,4)
    console.log(a)//和push一样
    a.pop()
    a.splice(-1,1)
    console.log(a)//和pop一样
    a.shift()
    a.splice(0,1)
    console.log(a)
    a.unshift(1)
    a.splice(0,0,1)
    console.log(a)
    

    2、写一个函数,操作数组,数组中的每一项变为原来的平方,在原数组上操作

    var a=[1,2,3,4],b
    b=a.map(function(x){return x*x})
    console.log(b)
    
    var a=[1,2,3,4]
    function aa(a){
      for(var i=0;i<a.length;i++){
        a[i]=a[i]*a[i]
      }
      return a
    }
    console.log(aa(a))
    

    3.写一个函数,操作数组,返回一个新数组,新数组中只包含正数,原数组不变

    var a=[1,2,3,4,-1,0,2,5],b
    b=a.filter(function(x){return x>0})
    console.log(b)
    
    var a=[1,2,3,4,-1,0,2,5]
    function positiveNumber(arr){
      var b=[],j=0
      for(var i in arr){
        if(arr[i]>0){
          b[j]=arr[i]
          j++
        }
      }
      return b
    }
    console.log(positiveNumber(a))
    

    1、 写一个函数getChIntv,获取从当前时间到指定日期的间隔时间

    function getChlntv(x){
       var a=Date.parse(x)-Date.now()
       var b=1000*60*60*24
       var c=1000*60*60
       var d=1000*60
       var e=1000
       if(a>=0){
         console.log('距离'+x+'还有'+Math.floor(a/b)+'天'+Math.floor(a%b/c)+'小时'+Math.floor(a%b%c/d)+'分'+Math.floor(a%b%c%d%e)+'秒')
       } else{
        console.log('从'+x+'到如今,我们已经走过'+Math.floor((-a)/b)+'天'+Math.floor((-a)%b/c)+'小时'+Math.floor((-a)%b%c/d)+'分'+Math.floor((-a)%b%c%d/e)+'秒')
      }
    }
    

    2.把hh-mm-dd格式数字日期改成中文日期

    var  str = getChsDate('2015-10-08');
      function getChsDate(str){
        var dict=        {'0':'零','1':'一','2':'二','3':'三','4':'四','5':'五','6':'六','7':'七','8':'八','9':'九','10':'十','11':'十一','12':'十二','13':'十三','14':'十四','15':'十五','16':'十六','17':'十七','18':'十八','19':'十九','20':'二十','21':'二十一','22':'二十二','23':'二十三','24':'二十四','25':'二十五','26':'二十六','27':'二十七','28':'二十八','29':'二十九','30':'三十','31':'三十一'}
        var arr=str.split('-')
        console.log(arr)
        var strYear='',strMonth,strDay,Date
        for(var i=0;i<arr[0].length;i++){
          strYear+=dict[arr[0][i]]
        }
        console.log(strYear)
        if(arr[1][0]==='0'){
          strMonth=dict[arr[1][1]]
        }else{
          strMonth=dict[arr[1]]
        }
        console.log(strMonth+'月')
        if(arr[2][0]==='0'){
           strDay=dict[arr[2][1]]
        }else{
           strDay=dict[arr[2]]
        }
        console.log(strDay)
        return strYear+'年'+strMonth+'月'+strDay+'日'
      }
    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 abc(time){
      var a=Date.now()-time
      console.log(a)
      switch(true){
          case a<1000:
          return '刚刚'
          break;
        case 1000<=a<1000*60*60:
          return '3分钟前';
          break;
        case 1000*60*60<=a<1000*60*60*24:
          return '8小时前';
          break;
        case 1000*60*60*24<=a<1000*60*60*24*30:
          return '3天前'
          break;
        case 1000*60*60*24*30<=a<1000*60*60*24*30*12:
          return '8个月前';
          break;
        case a>=1000*60*60*24*30*12:
          return '8年前';
      }
    }
    

    相关文章

      网友评论

          本文标题:Math数组Date

          本文链接:https://www.haomeiwen.com/subject/zvrwqxtx.html