Math任務
- 寫一個函數,返回從min到max之間的 隨機整數,包括min不包括max
function getRandInt(min,max){
return Math.floor(Math.random()*(max-min))+min
}
getRandInt(1,10)
- 寫一個函數,返回從min都max之間的 隨機整數,包括min包括max
function getRandInt2(min,max){
return Math.floor(Math.random()*(max-min)+1)+min
}
getRandInt2(1,10)
- 寫一個函數,生成一個長度為 n 的隨機字符串,字符串字符的取值范圍包括0到9,a到 z,A到Z。
function getRandStr(len){
var str='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
var newStr=''
for(i=0;i<len;i++){
newStr+=str[Math.floor(Math.random()*str.length)]
}
return newStr
}
getRandStr(10)
- 寫一個函數,生成一個隨機 IP 地址,一個合法的 IP 地址為 0.0.0.0~255.255.255.255
function getRandIP(){
var arr=[]
for(var i=0;i<4;i++){
// arr[i]=Math.floor(Math.random()*256)
arr.push(Math.floor(Math.random()*256))
}
return arr.join(".")
}
- 寫一個函數,生成一個隨機顏色字符串,合法的顏色為#000000~ #ffffff
function getRandColor(){
var str='abcdef0123456789'
var newStr=''
for(var i=0;i<6;i++){
newStr+=str[Math.floor(Math.random()*str.length)]
}
return '#'+newStr
}
數組任務
1. 數組方法里push、pop、shift、unshift、join、splice分別是什么作用?用 splice函數分別實現push、pop、shift、unshift方法
- push方法用于在數組的末端添加一個或多個元素,并返回添加新元素后的數組長度。注意,該方法會改變原數組
var arr = []
arr.push(1,2)
arr.push("a")
arr.push({})
arr //[1,2,"a",{}]
- pop方法用于刪除數組的最后一個元素,并返回該元素。注意,該方法會改變原數組。
var arr = [3,4,5]
arr.pop()
arr //[3,4]
- shift方法用于刪除數組的第一個元素,并返回該元素。注意,該方法會改變原數組。
var arr = [3,4,5]
arr.shift()
arr //[4,5]
- unshift方法用于在數組的第一個位置添加元素,并返回添加新元素后的數組長度。注意,該方法會改變原數組。
var arr = []
arr.unshift({})
arr.unshift(1)
arr //[1,{}]
- join方法以參數作為分隔符,將所有數組成員組成一個字符串返回。如果不提供參數,默認用逗號分隔
var a = [1, 2, 3, 4];
a.join(' ') // '1 2 3 4'
a.join(' | ') // "1 | 2 | 3 | 4"
a.join() // "1,2,3,4"
- splice方法用于刪除原數組的一部分成員,并可以在被刪除的位置添加入新的數組成員,返回值是被刪除的元素。注意,該方法會改變原數組。
var arr = [3,7,24,7]
arr.splice(arr.length,0,1,2) //相當于arr.push(1,2)
arr.splice(arr.length-1,1,) //相當于arr.pop()
arr.splice(0,0,12) //相當于arr.unshift(12)
arr.splice(0,1) //相當于arr.shift()
2. 寫一個函數,操作數組,數組中的每一項變為原來的平方,在原數組上操作
function squareArr(arr){
arr.forEach(function(e,i,array){
array[i]=e*e
})
return arr
}
var arr = [2,4,5]
squareArr(arr)
3. 寫一個函數,操作數組,返回一個新數組,新數組中只包含正數,原數組不變
function filterPositive(arr){
var newArr = arr.filter(function(e){
return e>0 && typeof e =='number'
})
return newArr
}
var arr = [3, -1, 2, '饑人谷', true]
var newArr = filterPositive(arr)
console.log(newArr)
console.log(arr)
Date任務
- 寫一個函數getChIntv,獲取從當前時間到指定日期的間隔時間
function getChIntv(appointedDay){
var target = new Date(appointedDay)
var now = new Date()
var totalmillisecond = Math.abs(target-now)
var totalSeconds = Math.floor(totalmillisecond/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 day = Math.floor(totalHours/24)
return '距離'+appointedDay+'還有'+day+'天'+hours+'小時'+minutes+'分'+seconds+'秒'
}
getChIntv("2017-11-18")
- 把hh-mm-dd格式數字日期改成中文日期
function getChsDate(str){
var dict = ['零','一','二','三','四','五','六','七','八','九','十']
var arr = str.split('-')
arr[0]=dict[arr[0][0]]+dict[arr[0][1]]+dict[arr[0][2]]+dict[arr[0][3]]
if(arr[1][0]==='0'){
arr[1]=dict[arr[1][1]]
}
else{
if(arr[1][1]==='0'){
arr[1]=dict[10]
}
else{
arr[1]=dict[10]+dict[arr[1][1]]
}
}
if(arr[2][0]==='0'){
arr[2]=dict[arr[2][1]]
}
else if(arr[2][0]==='1'){
if(arr[2][1]==='0'){
arr[2][0]=dict[10]
}
else{
arr[2]=dict[10]+dict[arr[2][1]]
}
}
else{
if(arr[2][1]==='0'){
arr[2]=dict[arr[2][0]]+dict[10]
}
else{
arr[2]=dict[arr[2][0]]+dict[10]+dict[arr[2][1]]
}
}
return arr[0]+'年'+arr[1]+'月'+arr[2]+'日'
}
getChsDate("2015-11-21")
- 寫一個函數,參數為時間對象毫秒數的字符串格式,返回值為字符串。假設參數為時間對象毫秒數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 now = Date.now()
var offset=Math.floor((now-time)/1000)
if(offset<60){
return '剛剛'
}
else if(offset>=3*60&&offset<60*60){
return '3分鐘前'
}
else if(offset>=60*60&&offset<24*60*60){
return '8小時前'
}
else if(offset>=24*60*60&&offset<30*24*60*60){
return '3天前'
}
else if(offset>=30*24*60*60&&offset<12*30*24*60*60){
return '2個月前'
}
else{
return '8年前'
}
}