美文网首页
Math数组Date

Math数组Date

作者: 饥人谷_Leon | 来源:发表于2017-07-02 22:45 被阅读0次

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

function getRand(min,max){
  return Math.floor(Math.random()*(max-min)) + min
}

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

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

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

function getRand(min,max) {
  return Math.floor(Math.random()*(max-min)) + min
}
function getRandStr(len) {
  var arr = [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"]
  var newarr = []
  for (var i = 0; i < len; i++) {
    newarr.push(arr[getRand(0,62)])
  }
return (newarr.join(''))
}
var str = getRandStr(10); // 0a3iJiRZap

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

function getRand(min,max){
  return Math.floor(Math.random()*(max-min+1))+min
}
function getRandIP(){
  arr = []
  for (var i = 1; i < 5; i++) {
    arr.push(getRand(0,255))
}
return (arr.join('.'))
}
var ip = getRandIP()
console.log(ip) // 10.234.121.45

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

function getRand(min,max){
  return Math.floor(Math.random()*(max-min+1))+min
}
function getRandColor(){
var colorstr = '#'
for (var i = 1; i < 6; i++){
  colorstr = colorstr + getRand(0,16).toString(16)
  }
return colorstr
}
var color = getRandColor()
console.log(color)   // #3e2f1b

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

  • push:在数组尾部添加一个或多个元素,语法:arr.push('a',2,3)改变原数组。
  • pop:在数组尾部删除一个元素,语法arr.pop(),返回该元素,改变原数组。
  • shift:在数组开头删除一个元素,语法:arr.shift(),返回该元素改变原数组。
  • unshift:在数组开头添加一个或多个元素,语法:arr.unshift('a',2,2,3)改变原数组。
  • join:将数组转变为字符串,语法arr.join(',')括号内参数为以什么符号分隔字符串,不改变原数组
  • splice:用于删除原数组的一部分成员,并可以在被删除的位置添加入新的数组成员,返回值是被删除的元素。,语法:arr.splice(1,2)第一个参数是删除的起始位置,第二个参数是被删除的元素个数。如果后面还有更多的参数,则表示这些就是要被插入数组的新元素。,改变原数组。

splice实现pop,shift,push,unshift

  • splice实现pop:arr.splice(-1,1)
  • splice实现shift:arr.splice(0,1)
  • splice实现push:arr.splice(arr.length,0,2,3,4,5,666)
  • splice实现unshift:arr.splice(0,0,2,3,4)

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

function squareArr(arr){
arr.forEach(function(elem,index,arr){
 arr[index] = elem*elem
})
}
var arr = [2,4, 6]
squareArr(arr)
console.log(arr) // [4, 16, 36]

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

function filterPositive(arr){
   return arr.filter(function(elem){
    return (elem > 0 && typeof elem === 'number')
  })
}
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(appointdate){
  var interval = Date.parse(appointdate) - Date.now()
  var d = Math.floor( interval / (24 * 60 * 60 *1000))
  var dr  = interval %  (24 * 60 * 60 *1000)
  var h = Math.floor( dr / (60 * 60 * 1000))
  var hr = dr %  (60 * 60 * 1000)
  var m = Math.floor( hr / ( 60 * 1000))
  var mr = hr % 60 *1000
  var s = mr / 1000
  return ( d + ' days ' + h + ' hours ' + m +' minutes ' + s + ' seconds ')
} 
var str = getChIntv("2018-07-08");
console.log(str);  // 距除夕还有 20 天 15 小时 20 分 10 秒

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

function getChsDate(numberdate){
  var arrch = ["零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一"]
  var datearr = numberdate.split('-')
  var yeararr = datearr[0].split('')
  var year = arrch[yeararr[0]] + arrch[yeararr[1]] + arrch[yeararr[2]] + arrch[yeararr[3]]
  var month = arrch[datearr[1]] 
  var day = arrch[datearr[2]]
  return (year + '年' + month + '月' + day + '日') 
}
var str = getChsDate('2015-11-28');
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 interval = Date.now() - time
  if ( interval < 6000 ) {
    console.log('刚刚')
  }else if ( interval < 60 * 60 * 1000){
    console.log( Math.floor( interval / ( 60 * 1000)) + '分钟前' )
  }else if ( interval < 24 * 60 * 60 * 1000){
    console.log( Math.floor( interval / ( 60 * 60 * 1000)) + '小时前' )
  }else if ( interval < 30 * 24 * 60 * 60 * 1000){
    console.log( Math.floor( interval / ( 24 * 60 * 60 * 1000)) + '天前' )
  }else if ( interval < 12 * 30 * 24 * 60 * 60 * 1000){
    console.log( Math.floor( interval / ( 30 * 24 * 60 * 60 * 1000)) + '个月前' )
  }else{
    console.log( Math.floor( interval / (12 * 30 * 24 * 60 * 60 * 1000)) + '年前' )
  }    
}
var str = friendlyDate( '1499311891955' ) //  1分钟前
var str2 = friendlyDate('1483941245793') //4天前

相关文章

  • 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/wkenqxtx.html