Rumah  >  Artikel  >  hujung hadapan web  >  关于Math、数组、Date的相关例子

关于Math、数组、Date的相关例子

jacklove
jackloveasal
2018-05-21 15:05:051488semak imbas

math,数组和date在学习中经常会遇到,本篇将会对其进行讲解。

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

function getRandom (min,max) {    return Math.floor(Math.random()*(max-min) + min)
}//Math.floor 返回小于参数值的最大整数//Math.random 返回[0,1)之间的随机数

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

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

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

function getRandomStr (n) {    var dict = '0123456789'+               'abcdefghijklnmopqrstuvwxyz'+               'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //一个字典;
    var string = &#39;&#39;;    for (i=0; i<n; i++) {
        string += dict[Math.floor(Math.random()*62)] ;
    }    return string ;
}

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

function getRandIP(){    var ipArr = [];    for (i=0; i<4; i++) {
        ipArr.push(Math.floor(Math.random()*256)); //push()方法把元素从尾部放进数组;
    }    return ipArr.join(&#39;.&#39;);
}var ip = getRandIP()console.log(ip)

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

function getColor () {    var dict = &#39;0123456789abcdef&#39;
    var color = &#39;#&#39;;    for (i=0; i<6; i++) {
        color += dict[Math.floor(Math.random()*16)];
    }    return color;
}
getColor() ;

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

push()方法可以把一个元素从尾部插入数组,改变原数组长度和索引;

var a =[] ;
a.push(4);
a ; // [4]

pop()方法可以删除并返回数组的最后一个元素,改变原数组长度和索引;

var a =[1,2] ;
a.pop(); // 2a ; // [1]

shift()方法可以把数组的第一个元素从其中删除,并返回第一个元素的值,改变原数组长度和索引;

var a=[1,2,3,4];
a.shift(); //1;a; //[2,3,4]

unshift()方法可向数组的开头添加一个或更多元素,并返回新的长度,改变原数组长度和索引;

var a=[1,2,3,4,5];
a.unshift(7,8); //7a; //[7,8,1,2,3,4,5]

splice()方法可以向/从数组中添加/删除项目,然后返回被删除的项目,没有删除则返回空数组,改变原数组长度和索引;

var a=[1,2,3];
a.splice(0,1); //[1];  //从第0位开始,删除1位;a; //[2,3]
var a=[1,2,3];
a.splice(0,0,7,8); //[];从第0位开始,删除0位,插入7,8(从前插入);a; //[7,8,1,2,3];
var a=[1,2,3,4];
a.splice(1,1,7,8); //[2];从第1位开始,删除1位([2]),并插入7,8;a; //[1,7,8,3,4];

用splice()可以实现push()、pop()、shift()、unshift()这些方法;

var a =[1,2,3,4,5,6];
a.splice(a.length,0,n) // 实现push(),n为想要传入的元素;a.splice(a.length-1,1)// 实现pop();a.splice(0,1) // 实现shift();a.splice(0,0,n) // 实现unshift(),n为想要传入的元素;

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

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]

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

function filterPositive(arr){    var newArr = [];    var k = 0;    for (i=0; i<arr.length; i++) {        if (typeof arr[i] === &#39;number&#39;) {  //验证是否为数字
            if(arr[i]>0) {
                newArr[k] = arr[i];
                k++;
            }
        }
    }    return newArr;
}var arr = [3, -1,  2,  &#39;饥人谷&#39;, true]var newArr = filterPositive(arr)console.log(newArr) //[3, 2]console.log(arr) //[3, -1,  2,  &#39;饥人谷&#39;, true]

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

//示例var str = getChIntv("2017-02-08");console.log(str);  // 距除夕还有 20 天 15 小时 20 分 10 秒
 function getChIntv(date) {    var time = Math.abs(Date.now()-Date.parse(date));    var day = Math.floor(time/86400000);    var hour = Math.floor(time%86400000/3600000);    var min = Math.floor(time%86400000%3600000/60000)    var s = Math.floor(time%86400000%3600000%60000/1000)    var ms = (time%86400000%3600000%60000%1000);    return &#39;距离&#39;+date+&#39;: &#39;+day+&#39; 天 &#39;+hour+&#39; 小时 &#39;+min+&#39; 分钟 &#39;+s+&#39; 秒 &#39;+ms+&#39; 毫秒 &#39;}
getChIntv(&#39;2017-04-08&#39;)  //距离2017-04-08: 55 天 4 小时 0 分钟 45 秒 774 毫秒;

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

//示例var str = getChsDate(&#39;2015-01-08&#39;);console.log(str);  // 二零一五年一月八日
var dict ={0:&#39;零&#39;,1:&#39;一&#39;,2:&#39;二&#39;,3:&#39;三&#39;,4:&#39;四&#39;,5:&#39;五&#39;,6:&#39;六&#39;,7:&#39;七&#39;,8:&#39;八&#39;,9:&#39;九&#39;,            10:&#39;十&#39;,11:&#39;十一&#39;,12:&#39;十二&#39;,13:&#39;十三&#39;,14:&#39;十四&#39;,15:&#39;十五&#39;,16:&#39;十六&#39;,            17:&#39;十七&#39;,18:&#39;十八&#39;,19:&#39;十九&#39;,20:&#39;二十&#39;,21:&#39;二十一&#39;,22:&#39;二十二&#39;,23:&#39;二十三&#39;,            24:&#39;二十四&#39;,25:&#39;二十五&#39;,26:&#39;二十六&#39;,27:&#39;二十七&#39;,28:&#39;二十八&#39;,29:&#39;二十九&#39;,30:&#39;三十&#39;,31:&#39;三十一&#39;} function getCHT (date) {
    time = date.split(&#39;-&#39;);
    year = time[0].split(&#39;&#39;);    for (i=0; i<year.length; i++) {
        year[i] = dict[year[i]];
    }
    month = dict[parseInt(time[1])];
    day = dict[parseInt(time[2])];    return year.join(&#39;&#39;)+&#39; 年 &#39;+month+&#39; 月 &#39;+day+&#39; 日&#39;}
getCHT(&#39;2017-06-01&#39;) // 二零一七年六月一日;

写一个函数,参数为时间对象毫秒数的字符串格式,返回值为字符串。假设参数为时间对象毫秒数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( &#39;1484286699422&#39; ) //  1分钟前var str2 = friendlyDate(&#39;1483941245793&#39;) //4天前
function friendlyDate(time){    var nowT = Date.now();  //Date.now()获取当前时间距离1970年1月1日00:00:00的毫秒数
    pastT = nowT-time;    if (pastT<=60*1000) {        return &#39;刚刚&#39;
    }if (pastT>60000 && pastT<3600*1000) {        return Math.floor(pastT/60000)+&#39;分钟前&#39;
    }if (pastT>=3600*1000 && pastT<24*3600*1000) {        return Math.floor(pastT/(3600*1000))+&#39;小时前&#39;
    }if (pastT>=24*3600*1000 && pastT<30*24*3600*1000) {        return Math.floor(pastT/(24*3600*1000))+&#39;天前&#39;
    }if (pastT>=30*24*3600*1000 && pastT<12*30*24*3600*1000) {        return Math.floor(pastT/(30*24*3600*1000))+&#39;月前&#39;
    }else {        return Math.floor(pastT/(360*24*3600*1000))+&#39;年前&#39;
    }
}
friendlyDate(1496246400000) //一天前;

本篇详解math、数组和date相关内容和例子,更多的相关内容请关注php中文网。

相关推荐:

HTML5/CSS3相关的知识讲解

Javascript操作DOM常用API总结

JavaScript全总结之定时器&DOM的document

Atas ialah kandungan terperinci 关于Math、数组、Date的相关例子. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn