美文网首页
Math数组 Date

Math数组 Date

作者: peaceChierdo | 来源:发表于2017-08-08 15:37 被阅读0次

Math任务

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

function havemin(min,max){
    return Math.floor(Math.random()*(max-min))+min
}
console.log(havemin(1,9))

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

function allinteger(min,max){
    return Math.floor(Math.random()*(max-min+1))+min
}
console.log(allinteger(1,9))

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

function getRandStr(len){
  var dict='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  var str='';
  var arr=dict.split('')
  for(var i=0;i<len;i++){
    str+=arr[Math.floor(Math.random()*62)]
  }
  return str
}
var str = getRandStr(10); // 0a3iJiRZap
console.log(str)

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

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

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

function getRandColor(){
    var arr=[]
    var dict='0123456789abcdef'
    arr.push('#')
    for(var i=0;i<6;i++){
        arr.push(dict[Math.floor(Math.random()*16)])    //= =   
    }
    return arr.join('')

}
var color = getRandColor()
console.log(color)   

数组任务

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

http://blog.csdn.net/aerchi/article/details/25238119

  • push 和 pop
    这两个函数都是对数组从尾部进行压入或弹出操作。push(arg1,arg2,...)可以每次压入一个或多个元素,并返回更新后的数组长度。注意如果参数也是数组的话,则是将全部数组当做一个元素压入到原本的数组里面去。pop() 函数则每次只会弹出结尾的元素,并返回弹出的元素,若是是对空组数调用 pop() 则返回undefined。
  • shift() 用于将数组的第一个元素从原数组中删除,并返回第一个元素的值(即被删除的元素的值)。
    unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。
  • join()作用是将数组各个元素是通过指定的分隔符进行连接成为一个字符串
  • splice()splice方法用于一次解决数组的添加、删除。
    三个参数:1.开始操作的元素下标 2.删除元素的位移 3.插入的新元素,当然也可以写多个
var arr=[1,3,5,2,4,6,9]
arr.splice(arr.length,0,7,7,7) //push()
arr.splice(arr.length-1,1) //pop()
arr.splice(0,1) //shift()
arr.splice(0,0,7,7,7) //unshift()

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

function squareArr(arr){
    for(var i=0;i<arr.length;i++){
        arr[i]*=arr[i]
    }
}
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(arr[i]>0&&typeof(arr[i])==='number'){
            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(time){
    var now = new Date()
    //console.log('now:'+now)
    var then= new Date(time)
    //console.log('then:'+then)
    var intv= Math.abs(now-then)
    //console.log('intv:'+intv)
    var ab='还有'
    if((now-then)>0){
        ab='已经过了'
    }
    var ms= Math.floor(intv%1000)
    var s=  Math.floor((intv%(1000*60)/1000))
    var min= Math.floor((intv%(1000*60*60))/(1000*60))
    var h= Math.floor((intv%(1000*60*60*24))/(1000*60*60))
    var d= Math.floor(intv/(1000*60*60*24))    
    return ('距离'+time+ab+d+'天'+h+'时'+min+'分钟'+s+'秒'+ms+'毫秒')
}
var str = getChIntv("2017-02-08");
console.log(str); 

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

function getChsDate(time){
    var dict=["零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一"]
    var arr=time.split('-')
    var part=[]
    for(var i=0;i<arr.length;i++){
        part[i]=arr[i].split('')
        for(var j=0;j<part[i].length;j++){
            if(part[i][j]=='0'&&(i!=0)&&(j!=1)){
                part[i][j]=''
            }
            part[i][j]=dict[part[i][j]]
            }
        }
        return part[0].join('')+'年'+part[1].join('')+'月'+part[2].join('')+'日'
    }
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()
    console.log(now)
    var t=now-time
    if(t<1000*60){  
        return '刚刚'
    }else if(t<1000*60*60){
        return '3分钟前'
    }else if(t<1000*60*60*24){
        return '8小时前'
    }else if(t<1000*60*60*24*30){
        return '3天前'
    }else if(t<1000*60*60*24*30*12){
        return '2个月前'
    }else{
        return '8年前'
    }
}
var str = friendlyDate( '1502174345728' ) //  1分钟前
var str2 = friendlyDate('1483941245793') //4天前
console.log(str)
console.log(str2)
//具体版
function friendlyDate(time){
   var now=Date.now()
   console.log(now)
   var t=now-time
   var s=1000,
       m=s*60,
       h=m*60,
       d=h*24,
       mon=d*30,
       y=mon*12;
   if(t<m){
       return '刚刚'
   }else if(t<=h){
       return Math.floor(t/m)+'分钟前'
   }else if(t<=d){
       return Math.floor(t/h)+'小时前'
   }else if(t<=mon){
       return Math.floor(t/d)+'天前'
   }else if(t<=y){
       return Math.floor(t/mon)+'个月前'
   }else{
       return Math.floor(t/y)+'年前'
   }

}
var str = friendlyDate( '1502174345728' ) 
var str2 = friendlyDate('1483941245793') 
console.log(str)
console.log(str2)
var aa=new Date(1483941245793)
var bb=aa.toString()
console.log(bb)

相关文章

  • JavaScript-对象

    Number 字符串 数组 Date日期 Math

  • Math数组,date

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

  • Math,Date,数组

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

  • Math数组Date

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

  • Math数组Date

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

  • Math 数组 Date

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

  • Math数组Date

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

  • Math数组Date

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

  • Math数组Date

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

  • Math数组Date

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

网友评论

      本文标题:Math数组 Date

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