Math数组Date

2017-06-20  本文已影响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方法。

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年前';
  }
}
上一篇下一篇

猜你喜欢

热点阅读