ES5数组字符串Date

2017-09-03  本文已影响0人  leiuno

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

function random(min,max){
    var num = Math.random() * (max - min) + min;
    return Math.floor(num);
}
var result = random(3,9); 
console.log(result);

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

function getRandStr(len){
    var str = "";
    var dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for(var i = 0; i < len; i++){
        var index = Math.floor(Math.random() * dict.length);
        str += dict[index];
    }
    return str;
}
var str = getRandStr(10); // 0a3iJiRZap

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

function adr(){
    var numIP = [];
    for(var i = 0; i < 4; i++){
        var num = Math.floor(Math.random() * 255);
        numIP.push(num);
    }
    var strIP = numIP.join(".");
    return strIP;
}
adr();

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

function getRandColor(){
    var dict = "0123456789abcdefghijklmnopqrstuvwxyz";
    var color = "#";
    for(var i = 0; i < 6; i++){
        var index = Math.floor(Math.random() * dict.length);
        color += dict[index];
    }
    return color;
}
getRandColor();

5.实现一个flatten函数,将一个嵌套多层的数组 array(数组) (嵌套可以是任何层数)转换为只有一层的数组,数组中元素仅基本类型的元素或数组,不存在循环引用的情况。

var newArr = [];
function flatten(arr){
    for(var i = 0; i < arr.length; i++){
        if(Array.isArray(arr[i])){
            flatten(arr[i]);
        }else{
            newArr.push(arr[i]);
        }
    }
    return newArr;
}
var arr = [1, [2], [3, [[4]]]];
flatten(arr);

6.实现一个reduce函数,作用和原生的reduce类似

function reduce(arr,callback,intialValue){
    var accumulator = intialValue;
    var startIndex = 0;
    if(typeof intialValue == "undefined"){
        accumulator = arr[0];
        startIndex = 1;
    }
    for(var i = startIndex; i < arr.length; i++){
        accumulator = callback(accumulator,arr[i],i,arr)
    }
    return accumulator;
}
reduce([1, 2, 3], function (a, b, c, d) {
    return a + b
}, 100)

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

function getInterval(fixationTime){
    var nowTime = Date.now();
    var endTime = Date.parse(fixationTime);
    var interval = Math.abs(nowTime - endTime);
    var intervalStr = new Date(interval);
    var year = intervalStr.getFullYear() - 1970;
    var month = intervalStr.getMonth();
    var date = intervalStr.getDate();
    var hour = intervalStr.getHours();
    var minute = intervalStr.getMinutes();
    var second = intervalStr.getSeconds();
    var text = "时间间隔:"+ year + "年" + month + "月" + date + "天"
               + hour + "小时" + minute + "分" + second + "秒"
    return text;
}
var str = getInterval("2017-02-08 10:30:24");
console.log(str);

8.写一个函数,参数为时间对象毫秒数的字符串格式,返回值为字符串。假设参数为时间对象毫秒数t,根据t的时间分别返回如下字符串:

function friendlyDate(t){
    if(typeof t !== "number" || isNaN(NaN)){
        return "参数非法"
    }
    var nowTime = Date.now();
    var interval = nowTime - t;
    var oneMinute = 1*60*1000;
    var oneHour = 1*60*60*1000;
    var oneDay = 24*60*60*1000;
    var oneMonth = 30*24*60*60*1000;
    var oneYear = 12*30*24*60*60*1000;
    var text;
    if(interval < 0){
        text = "传入的是未来时间"
    }else if(interval < oneMinute){
        text = "刚刚"
    }else if(interval >= oneMinute && interval < oneHour){
        text = "3分钟前"
    }else if(interval >= oneHour && interval < oneDay){
        text = "8小时前"
    }else if(interval >= oneDay && interval < oneMonth){
        text = "3天前"
    }else if(interval >= oneMonth && interval < oneYear){
        text = "2个月前"
    }else if(interval > oneYear){
        text = "8年前"
    }
    return text;
}
//var str = friendlyDate(Date.parse('2018-09-03T03:25:00'));
上一篇下一篇

猜你喜欢

热点阅读