美文网首页
进阶任务6-Math数组Date

进阶任务6-Math数组Date

作者: cheneyzhangch | 来源:发表于2017-06-17 17:52 被阅读0次

Math任务

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

<script>
  function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
  }
  var arr = []
  for (i = 1; i < 100; i++) {
    arr.push(getRandomInt(5, 10))
  }
  console.log(arr)
</script>

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

<script>
  function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  var arr = []      //取100组数据进行检查
  for (i = 1; i < 100; i++) {
    arr.push(getRandomInt(5, 10))
  }
  console.log(arr)
</script>

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

<script>
  function getRandStr(len) {
    var totalletter = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
    var username = []
    function getRandomLetter() {
      return Math.floor(Math.random() * 62)
    }
    for (i = 0; i < len; i++) {
      username.push(totalletter[getRandomLetter()])
    }
    return username.join('')
  }
  console.log(getRandStr(8))
</script>

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

<script>
  function getRandomIp() {
    var newIp = []
    for (j = 0; j < 4; j++) {
      newIp.push(Math.floor(Math.random() * 256))
    }
    return newIp.join('.')
  }
  console.log(getRandomIp())
  var ipArray = []      //取100组数据进行检查
  for (i = 0; i < 100; i++) {
    ipArray.push(getRandomIp())
  }
  console.log(ipArray)

</script>

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

function getRandColor(){
}
var color = getRandColor()
console.log(color) // #3e2f1b

<script>
  function getRandomColor() {
    var totalletter = '0123456789abcdef'.split('')
    var username = []
    function getRandomLetter() {
      return Math.floor(Math.random() * 16)
    }
    for (i = 0; i < 6; i++) {
      username.push(totalletter[getRandomLetter()])
    }
    return '#' + username.join('')
  }
  console.log(getRandomColor())
  var colorArray = []
  for (j = 0; j < 100; j++) {
    colorArray.push(getRandomColor())
  }
  console.log(colorArray)

</script>

数组任务

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

  • arr.push() 在数组的最后添加内容
  • arr.pop() 从数组的最后去除内容
  • arr.shift() 从数组的前面拿走东西
  • arr.unshift() 在数组的前面塞进东西
  • arr.jion('') 连接数组成字符串
  • arr.splice(开始索引,删除元素的个数,插入的元素) 插入的元素可以有多个,以达到修改数组的目的
<script>
  var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
  var arr2 = [1, 2, 3, 4, 5, 6, 7, 8]
  arr1.push(0)
  arr2.splice(arr2.length, 0, 0)
  console.log(arr1, arr2)
  console.log(arr1.join('') === arr2.join(''))
  arr1.pop()
  arr2.splice(arr2.length-1, 1)
  console.log(arr1, arr2)
  console.log(arr1.join('') === arr2.join(''))
  arr1.shift()
  arr2.splice(0, 1)
  console.log(arr1, arr2)
  console.log(arr1.join('') === arr2.join(''))
  arr1.unshift(1)
  arr2.splice(0, 0, 1)
  console.log(arr1, arr2)
  console.log(arr1.join('') === arr2.join(''))
</script>

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

<script>
 function squareArr(arr){
  for (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]
</script>

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

<script>
function filterPositive(arr){
  return arr.filter(function(value){
    return value > 0 && typeof value === 'number'
  })
}
var arr = [3, -1,  2,  '饥人谷', true]
var newArr = filterPositive(arr)
console.log(newArr) //[3, 2]
console.log(arr) //[3, -1,  2,  '饥人谷', true]
</script>

Date 任务

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

<script>
  function getChIntv(str) {
    var targetDate = new Date(str)
    var curentDate = new Date()
    var offset = Math.abs(targetDate - curentDate)
    var totalSeconds = Math.floor(offset / 1000)
    var seconds = Math.floor(totalSeconds % 60)
    var totalMinutes = Math.floor(totalSeconds / 60)
    var minutes = Math.floor(totalMinutes % 60)
    var totalHours = Math.floor(totalMinutes / 60)
    var hours = Math.floor(totalHours % 24)
    var totalDays = Math.floor(totalHours / 24)
    return totalDays + '天' + hours + '小时' + minutes + '分钟' + seconds + '秒'
  }
  var str = getChIntv("2017-02-08");
 console.log(str);  // 距除夕还有 20 天 15 小时 20 分 10 秒
</script>

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

var str = getChsDate('2015-01-08');
console.log(str); // 二零一五年一月八日

<script>
  function getChsDate(str) {
    var data = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十", "三十一"];
    var d = new Date(str)
    var yearEn = d.getFullYear()
    var years = yearEn.toString().split('')
    var monthEn = d.getMonth() + 1
    var dayEn = d.getDate()
    var yearChs = ''
    for (i = 0; i < years.length; i++) {
      yearChs += data[years[i]]
    }
    console.log(yearChs + '年' + data[d.getMonth() + 1] + '月' + data[d.getDate()] + '日')
    return yearChs + '年' + data[d.getMonth() + 1] + '月' + data[d.getDate()] + '日'
  }
  getChsDate('2017-02-10')

</script>

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 str = friendlyDate( '1484286699422' ) // 1分钟前
var str2 = friendlyDate('1483941245793') //4天前

<script>
  function getChIntv(str) {
    var targetDate = new Date(str)
    var curentDate = new Date()
    var offset = Math.abs(targetDate - curentDate)
    var totalSeconds = Math.floor(offset / 1000)
    var seconds = Math.floor(totalSeconds % 60)
    var totalMinutes = Math.floor(totalSeconds / 60)
    var minutes = Math.floor(totalMinutes % 60)
    var totalHours = Math.floor(totalMinutes / 60)
    var hours = Math.floor(totalHours % 24)
    var totalDays = Math.floor(totalHours / 24)
    if (totalSeconds < 60) {
      return '刚刚'
    } else {
      if (totalMinutes < 10) {
        return '几分钟前'
      } else {
        if (totalMinutes < 60) {
          return '一小时前'
        } else {
          if (totalHours < 24) {
            return '一天前'
          } else {
            if (totalDays < 30) {
              return '一个月前'
            } else {
              if (totalDays < 365) {
                return '一年前'
              } else {
                return '几年前'
              }
            }
          }
        }
      }
    }
  }
  var str = getChIntv("2017-06-17 16: 45:00");
  console.log(str);  // 距除夕还有 20 天 15 小时 20 分 10 秒

</script>


相关文章

  • 进阶任务6-Math数组Date

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

  • 进阶任务6-Math数组Date

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

  • 进阶任务6:Math数组Date

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

  • 进阶任务6(主线任务):Math数组Date

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

  • 进阶任务6(主线任务):Math数组Date

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

  • 进阶六(Math数组Date)课程任务

    math任务 1.写一个函数,获取从min到max之间的随机整数,包括min不包括max 2.写一个函数,获取从m...

  • 进阶六 Math 数组 Date

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

  • 进阶6 Math数组Date

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

  • 进阶6 Math·数组·Date

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

  • 进阶6:Math数组Date

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

网友评论

      本文标题:进阶任务6-Math数组Date

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