Math
1. 寫一個(gè)函數(shù)臼勉,返回從min到max之間的隨機(jī)整數(shù)邻吭,包括min不包括max
function randomArr(min,max){
return min+Math.floor(Math.random()*(max-min))
}
2. 寫一個(gè)函數(shù),返回從min都max之間的隨機(jī)整數(shù)宴霸,包括min包括max
function randomArr(min,max){
return min+Math.floor(Math.random()*(max-min+1)
}
3.寫一個(gè)函數(shù)囱晴,生成一個(gè)長度為 n 的隨機(jī)字符串,字符串字符的取值范圍包括0到9瓢谢,a到 z畸写,A到Z
function getRandStr(len){
function random(min,max){
return min+Math.floor(Math.random()*(max-min))
}
var dict='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var str='';
for(i=0;i<len;i++){
str+=dict[random(0,62)]
}
return str
}
var str=getRandStr(10);
console.log(str);
1.png
4.寫一個(gè)函數(shù),生成一個(gè)隨機(jī) IP 地址氓扛,一個(gè)合法的 IP 地址為 0.0.0.0~255.255.255.255
function getRandIp(){
function random(min,max){
return min+Math.floor(Math.random()*(max-min))
}
var arr=[];
for(i=0;i<4;i++){
arr.push(random(0,256))
}
return arr.join('.')
}
var ip=getRandIp()
console.log(ip)
2.png
5.寫一個(gè)函數(shù)枯芬,生成一個(gè)隨機(jī)顏色字符串,合法的顏色為#000000~ #ffffff
function getRandColor(){
function random(min,max){
return min+Math.floor(Math.random()*(max-min))
}
var dict='0123456789abcdef';
var str='#';
for(i=0;i<6;i++){
str+=dict[random(0,16)]
}
return str
}
var color=getRandColor()
console.log(color)
3.png
數(shù)組
1.數(shù)組方法里push采郎、pop千所、shift、unshift蒜埋、join淫痰、splice分別是什么作用?用 splice函數(shù)分別實(shí)現(xiàn)push整份、pop待错、shift、unshift方法
方法 | 用法 | 作用 |
---|---|---|
push | Array.push(str) | 將一個(gè)或多個(gè)元素添加到數(shù)組的末尾皂林,并返回新數(shù)組的長度 |
pop | Array.pop() | 將數(shù)組末尾最后一個(gè)元素刪去朗鸠,并返回該元素的值(更改數(shù)組長度) |
shift | Array.shift() | 將數(shù)組的第一個(gè)元素刪除蚯撩,并返回該元素的值(更改數(shù)組長度) |
unshift | Array.unshift() | 將一個(gè)或多個(gè)元素添加到數(shù)組的開頭础倍,并返回新的數(shù)組長度 |
join | Array.join(separator) | 將數(shù)組或類數(shù)組的所有元素以制定分隔符進(jìn)行連接,稱為一個(gè)字符串 |
splice | Array.splice(index,deleteCount,item1,item2,...) | 從指定位置(index)起胎挎,執(zhí)行若干次(deleteCount)刪除或者替換(替換內(nèi)容為item1,item2...)沟启,返回值為被刪除的部分組成的新數(shù)組 |
- 用splice實(shí)現(xiàn)push
var a=[2,5,8,4,5,6]
a.splice(a.length,0,'something')
console.log(a) //[2, 5, 8, 4, 5, 6, "something"]
- 用splice實(shí)現(xiàn)pop
var b=[1,2,3,4,5,6]
b.splice(-1,1)
console.log(b) //[1, 2, 3, 4, 5]
- 用splice實(shí)現(xiàn)shift
var c=[2,3,5,6,7]
c.splice(0,1)
console.log(c) //[3, 5, 6, 7]
- 用splice實(shí)現(xiàn)unshift
var d=[3,5,6,7,8]
d.splice(0,0,20)
console.log(d) //[20, 3, 5, 6, 7, 8]
2.寫一個(gè)函數(shù)忆家,操作數(shù)組,數(shù)組中的每一項(xiàng)變?yōu)樵瓉淼钠椒降录#谠瓟?shù)組上操作
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]
4.png
3.寫一個(gè)函數(shù)芽卿,操作數(shù)組,返回一個(gè)新數(shù)組胳搞,新數(shù)組中只包含正數(shù)卸例,原數(shù)組不變
function filterPositive(arr){
var newArr=[]
for(i=0;i<arr.length;i++){
if(arr[i]>0 && typeof(arr[i])==='number'){
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]
5.png
Date
1.寫一個(gè)函數(shù)getChIntv,獲取從當(dāng)前時(shí)間到指定日期的間隔時(shí)間
function getChIntv(time){
var targetDate=new Date(time);
var curDate=new Date();
var offset=curDate-targetDate;
var totalSeconds=Math.abs(offset/1000);
var seconds=Math.floor(totalSeconds%60);
var totalMinutes=totalSeconds/60;
var minutes=Math.floor(totalMinutes%60);
var totalHours=totalMinutes/60;
var hours=Math.floor(totalHours%24);
var days=Math.floor(totalHours/24);
if(offset<0){
return '距離'+time+'已經(jīng)過去'+days+'天'+hours+'小時(shí)'+minutes+'分鐘'+seconds+'秒'
}else{
return '距離'+time+'還有'+days+'天'+hours+'小時(shí)'+minutes+'分鐘'+seconds+'秒'
}
}
var str1 = getChIntv("2017-11-16(CST)");
var str2 = getChIntv("2017-11-15(CST)");
console.log(str1);
console.log(str2)
6.png
2.把hh-mm-dd格式數(shù)字日期改成中文日期
function getChsDate(time){
var arr=time.split('-');
var dict=['零','一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五','十六','十七','十八','十九','二十','二十一','二十二','二十三','二十四','二十五','二十六','二十七','二十八','二十九','三十','三十一'];
var years=arr[0];
var months=arr[1];
var days=arr[2];
var cnYear=dict[years[0]]+dict[years[1]]+dict[years[2]]+dict[years[3]];
var cnMonth=dict[parseInt(months)]
var cnDay=dict[parseInt(days)]
return cnYear+'年'+cnMonth+'月'+cnDay+'日'
}
var str = getChsDate('2015-01-08');
console.log(str);
3.寫一個(gè)函數(shù)肌毅,參數(shù)為時(shí)間對象毫秒數(shù)的字符串格式筷转,返回值為字符串。假設(shè)參數(shù)為時(shí)間對象毫秒數(shù)t悬而,根據(jù)t的時(shí)間分別返回如下字符串:
- 剛剛( t 距當(dāng)前時(shí)間不到1分鐘時(shí)間間隔)
- 3分鐘前 (t距當(dāng)前時(shí)間大于等于1分鐘呜舒,小于1小時(shí))
- 8小時(shí)前 (t 距離當(dāng)前時(shí)間大于等于1小時(shí),小于24小時(shí))
- 3天前 (t 距離當(dāng)前時(shí)間大于等于24小時(shí)笨奠,小于30天)
- 2個(gè)月前 (t 距離當(dāng)前時(shí)間大于等于30天小于12個(gè)月)
- 8年前 (t 距離當(dāng)前時(shí)間大于等于12個(gè)月)
function friendlyDate(time){
var offset=(Date.now()-time)/1000;
if(offset<60){
console.log('剛剛')
}else if(offset>=60 && offset<60*60){
console.log(Math.floor((offset)/60)+'分鐘前')
}else if(offset>=60*60 && offset<60*60*24){
console.log(Math.floor((offset)/(60*60))+'小時(shí)前')
}else if(offset>=60*60*24 && offset<60*60*24*30){
console.log(Math.floor((offset)/(60*60*24))+'天前')
}else if(offset>=60*60*24*30 && offset<=60*60*24*30*12){
console.log(Math.floor((offset)/(60*60*24*30))+'個(gè)月前')
}else if(offset>=60*60*24*30*12){
console.log(Math.floor((offset)/(60*60*24*30*12))+'年前')
}
}
var str = friendlyDate('1484286699422')
var str2 = friendlyDate('1510835761009')
console.log(str)
console.log(str2)
補(bǔ)充
1. 判斷閏年
function leapYear(year){
var d=new Date(year,1,29);
return d.getDate()===29
}
var a=leapYear(2008);
var b=leapYear(2009);
console.log(a); //true
console.log(b); //false
2. 怎樣獲取月份相應(yīng)天數(shù)(包括平年和閏年)
function getDaysOfMonth(year,month){
var a=new Date(year,month,1,0,0,0);
var days=new Date(a-1000);
return days.getDate()
}
console.log(getDaysOfMonth(2008,2))