Math数组Date

2017-06-19  本文已影响0人  annynick

Math任务

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

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

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

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

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

function random(min,max){
    return min + Math.floor(Math.random()*(max-min))
}
function getRandStr(len){
  var dict="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  var str=""
  for(var i=0;i<len;i++){
      str+=dict[random(0,62)]
  }
  return str
}
var str = getRandStr(10);
console.log(str)

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

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

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

function random(min,max){
    return min + Math.floor(Math.random()*(max-min))
}
function getRandColor(){
    var dict="0123456789abcdef"
    var colorvalue=''
   for(var i=0;i<6;i++){
    colorvalue+=dict[random(0,16)]
   }
   return "#"+colorvalue
}
var color = getRandColor()
console.log(color)

数组任务

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

  var arr = [1,2,3,4,5,6];
    arr.splice(arr.length, 0, 7);
    console.log(arr); //push

    arr = [1,2,3,4,5,6];
    arr.splice(arr.length - 1, 1)
    console.log(arr);    //pop

    arr = [1,2,3,4,5,6];
    arr.splice(0, 1)
    console.log(arr);    //shift

    arr = [1,2,3,4,5,6];
    arr.splice(0, 0, 0)
    console.log(arr);   //unshift

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

function squareArr(arr){
    arr.forEach(function(value,index){
        return arr[index]=value*value
    });
}
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(typeof arr[i]==="number"&& arr[i]>0){
            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(date){
    var targetDate=new Date(date)
    var localDate =new Date()
    var offtime=Math.abs(localDate-targetDate)
    var totalSeconds = Math.floor(offtime/1000)
    var seconds = totalSeconds%60
    var totalMinutes = Math.floor(totalSeconds/60)
    var minutes = totalMinutes%60
    var totalHours = Math.floor(totalMinutes/60)
    var hours = totalHours%24
    var days = Math.floor(totalHours/24)
    return "距离国庆节还有"+days+"天"+hours+"小时"+minutes+"分"+seconds+"秒"
}
var str = getChIntv("2017-10-01");
console.log(str);  

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

function getChsDate(str) {
    var date = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十", "三十一"];
    var d = new Date(str)
    var year = d.getFullYear()
    var years = year.toString().split("")
    var month = d.getMonth()+1
    var day = d.getDate()
    var yearCh="" 
    for(var i=0;i<years.length;i++){
       
        yearCh += date[years[i]]
    }
    return yearCh + '年' + date[month] + '月' + date[day] + '日'

  }

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 timeBefore(t){
    var nowSec=Math.floor(Date.now()/1000)
    var setSec=Math.floor(t/1000)
    var offSec= nowSec-setSec
    if(offSec<0){
        return "输入错误"
    }else if(offSec<60){
        return "刚刚" 
    }else if(offSec<60*60){
        return "3分钟前"
    }else if(offSec<60*60*24){
        return "8小时前"
    }else if(offSec<60*60*24*30){
        return "3天前"
    }else if(offSec<60*60*24*365){
        return "2个月前"
    }
    else{
         return "8年前"
    }

}
var str = timeBefore( '1497865400960' ) 
var str2 = timeBefore('1483941245793') 
console.log(str)
上一篇 下一篇

猜你喜欢

热点阅读