1、寫一個函數,返回從min到max之間的 隨機整數,包括min不包括max
function minMax(min,max){
return Math.floor(Math.random()*(max-min)+min)
}
var a=minMax(50,100)
console.log(a)
2.寫一個函數,返回從min都max之間的 隨機整數,包括min包括max
function minMax(min,max){
return Math.floor(Math.random()*(max-min+1)+min)
}
var a=minMax(20,30)
console.log(a)
3.寫一個函數,生成一個長度為 n 的隨機字符串,字符串字符的取值范圍包括0到9,a到 z,A到Z。
var dict=[0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
function rd(n){
var arr=[]
arr.length=n
for(i=0;i<arr.length;i++){
arr[i]=dict[Math.floor(Math.random()*dict.length)]
}
return arr.join('')
}
var str1=rd(29)
console.log(str1)
4 、寫一個函數,生成一個隨機 IP 地址,一個合法的 IP 地址為 0.0.0.0~255.255.255.255
function rdIp(){
arr=new Array(4)
for(var i=0;i<arr.length;i++){
arr[i]=Math.floor(Math.random()*256)
}
return arr.join('.')
}
var a=rdIp()
console.log(a)
5、寫一個函數,生成一個隨機顏色字符串,合法的顏色為#000000~ #ffffff
function rdColor(){
var dict=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'],color
arr=new Array(6)
for(var i=0;i<arr.length;i++){
arr[i]=dict[Math.floor(Math.random()*dict.length)]
}
return color='#'+arr.join('')
}
console.log(rdColor())
1.數組方法里push、pop、shift、unshift、join、splice分別是什么作用?用 splice函數分別實現push、pop、shift、unshift方法。
push
往數組最后面增添一個元素,會改變原數組 arr.push(要增加的元素);//返回增加的值pop
刪除數組最后面的一個元素,改變原數組,arr.pop();//返回刪除的值shift
刪除數組最前面的一個元素,改變數組,arr.shift();//返回刪除的值unshift
往數組最前面增添一個元素,改變原數組,arr.unshift(要增加的元素);//返回增加的值join
把數組中所有元素放入一個字符串,并且,可以指定分割符。
arr.join('-')//返回字符串-
splice
splice 中文:拼接,結合。 在js中可以操作數組,增加/刪除。
用法:splice(index,howmany,item1,item2...)//返回刪除的新數組
其中index,起始位置,支持負數,負數就是倒序(負數時,第二個參數沒用,只刪除一次),(必選參數)
howmany,刪除多少次。(必選參數)
items ,要增加的項目。(可選),改變原數組。- 容易和slice,split搞混(因為拼寫有點像啊)
- slice 切片,字符串和數組都可以用,a.slice(x,y)代表切片起始位置x和終止位置,支持負數。返回的是切片的字符串或者數組。
- split,將字符串轉換為數組,a.split(x,y) x選擇字符串或正則表達式來作為分割線,并且會去掉他們,y,可選。該參數可指定返回的數組的最大長度。如果設置了該參數,返回的子串不會多于這個參數指定的數組。如果沒有設置該參數,整個字符串都會被分割,不考慮它的長度。
用splice實現push,pop,shift,unshift
var a=[1,2,3,4,5],b
a.push(6)
a.splice(a.length,0,4)
console.log(a)//和push一樣
a.pop()
a.splice(-1,1)
console.log(a)//和pop一樣
a.shift()
a.splice(0,1)
console.log(a)
a.unshift(1)
a.splice(0,0,1)
console.log(a)
2、寫一個函數,操作數組,數組中的每一項變為原來的平方,在原數組上操作
var a=[1,2,3,4],b
b=a.map(function(x){return x*x})
console.log(b)
var a=[1,2,3,4]
function aa(a){
for(var i=0;i<a.length;i++){
a[i]=a[i]*a[i]
}
return a
}
console.log(aa(a))
3.寫一個函數,操作數組,返回一個新數組,新數組中只包含正數,原數組不變
var a=[1,2,3,4,-1,0,2,5],b
b=a.filter(function(x){return x>0})
console.log(b)
var a=[1,2,3,4,-1,0,2,5]
function positiveNumber(arr){
var b=[],j=0
for(var i in arr){
if(arr[i]>0){
b[j]=arr[i]
j++
}
}
return b
}
console.log(positiveNumber(a))
1、 寫一個函數getChIntv,獲取從當前時間到指定日期的間隔時間
function getChlntv(x){
var a=Date.parse(x)-Date.now()
var b=1000*60*60*24
var c=1000*60*60
var d=1000*60
var e=1000
if(a>=0){
console.log('距離'+x+'還有'+Math.floor(a/b)+'天'+Math.floor(a%b/c)+'小時'+Math.floor(a%b%c/d)+'分'+Math.floor(a%b%c%d%e)+'秒')
} else{
console.log('從'+x+'到如今,我們已經走過'+Math.floor((-a)/b)+'天'+Math.floor((-a)%b/c)+'小時'+Math.floor((-a)%b%c/d)+'分'+Math.floor((-a)%b%c%d/e)+'秒')
}
}
2.把hh-mm-dd格式數字日期改成中文日期
var str = getChsDate('2015-10-08');
function getChsDate(str){
var dict= {'0':'零','1':'一','2':'二','3':'三','4':'四','5':'五','6':'六','7':'七','8':'八','9':'九','10':'十','11':'十一','12':'十二','13':'十三','14':'十四','15':'十五','16':'十六','17':'十七','18':'十八','19':'十九','20':'二十','21':'二十一','22':'二十二','23':'二十三','24':'二十四','25':'二十五','26':'二十六','27':'二十七','28':'二十八','29':'二十九','30':'三十','31':'三十一'}
var arr=str.split('-')
console.log(arr)
var strYear='',strMonth,strDay,Date
for(var i=0;i<arr[0].length;i++){
strYear+=dict[arr[0][i]]
}
console.log(strYear)
if(arr[1][0]==='0'){
strMonth=dict[arr[1][1]]
}else{
strMonth=dict[arr[1]]
}
console.log(strMonth+'月')
if(arr[2][0]==='0'){
strDay=dict[arr[2][1]]
}else{
strDay=dict[arr[2]]
}
console.log(strDay)
return strYear+'年'+strMonth+'月'+strDay+'日'
}
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 abc(time){
var a=Date.now()-time
console.log(a)
switch(true){
case a<1000:
return '剛剛'
break;
case 1000<=a<1000*60*60:
return '3分鐘前';
break;
case 1000*60*60<=a<1000*60*60*24:
return '8小時前';
break;
case 1000*60*60*24<=a<1000*60*60*24*30:
return '3天前'
break;
case 1000*60*60*24*30<=a<1000*60*60*24*30*12:
return '8個月前';
break;
case a>=1000*60*60*24*30*12:
return '8年前';
}
}