Math任务
function getRandStr(min,max){
return min+Math.floor(Math.random()*(max-min))
}
function getRandStr(min,max){
return min+Math.floor(Math.random()*(max-min+1))
}
function random(a,b){
return a+Math.floor(Math.random()*(b-a))
}
function getRandStr(n){
var dict="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var str=''
for(i=0;i<n;i++){
str+=dict[random(0,62)]
}
return str
}
function random(a,b){
return a+Math.floor(Math.random()*(b-a))
}
function getRandIP(){
var arr=[]
for(i=0;i<4;i++){
arr.push(random(0,256))
}
return arr.join('.')
}
var ip = getRandIP()
console.log(ip)
function random(a,b){
return a+Math.floor(Math.random()*(b-a))
}
function getRandColor(){
var dict="0123456789abcdef"
var str="#"
for(i=0;i<6;i++){
str+=dict[random(0,16)]
}
return str
}
var color = getRandColor()
console.log(color)
数组任务
push在数组最后添加一个元素
pop把数组最后一个元素拿出来,原数组改变
shift把数组第一位拿出来,原数组改变
unshiift在数组第一位新增元素
join把数组连接成字符串
splice在数组里的任意位置添加删除元素
var arr=[1,2,3]
arr.splice(3,0,4)
console.log(arr) //[ 1, 2, 3, 4 ]
arr.splice(3,1)
console.log(arr) //[ 1, 2, 3 ]
arr.splice(0,0,'hello')
console.log(arr) //[ 'hello', 1, 2, 3 ]
arr.splice(0,1)
console.log(arr) //[ 1, 2, 3 ]
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)
function filterPositive(arr){
var newArr=[]
for(i=0;i<arr.length;i++){
if (typeof arr[i]=='number' && arr[i]>0){
newArr.push(arr[i])
}
}
return newArr
}
var arr = [3, -1, 2, '饥人谷', true]
var newArr = filterPositive(arr)
console.log(newArr)
console.log(arr)
Date任务
var str = getChIntv("2018-02-15");
function getChIntv(dateStr) {
var targetDate=new Date(dateStr);
var curDate=new Date();
var offset=Math.abs(targetDate-curDate);
var totalSeconds=Math.floor(offset/1000);
var seconds=totalSeconds%60;
var totalMinutes=Math.floor(totalSeconds/60)
var mintues=totalMinutes%60
var totalHours=Math.floor(totalMinutes/60)
var hours=totalHours%24
var totalDays=Math.floor(totalHours/24)
return '距离除夕还有'+totalDays+'天'+hours+'小时'+mintues+'分'+seconds+'秒'
}
console.log(str);
var str = getChsDate('2019-11-31');
function getChsDate(str) {
var dict=["零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一"]
var arr=str.split('-')
var year=arr[0]
var month=arr[1]
var day=arr[2]
var newyeararr=[]
for(i=0,j=0;i<year.length,j<year.length;i++,j++){
newyeararr[j]=dict[year[i]];
}
var newyearstr=newyeararr.join('')
var newmonth=dict[Math.floor(month)]
var newday=dict[Math.floor(day)]
return newyearstr+'年'+newmonth+'月'+newday+'日'
}
console.log(str);
function friendlyDate(time){
var now=Date.now()
var offSetSeconds=(now-time)/1000
if (offSetSeconds<60){
return '刚刚'
}else if(offSetSeconds<60*60){
return'3分钟前'
}else if(offSetSeconds<60*60*24){
return'8小时前'
}else if(offSetSeconds<60*60*24*30){
return'3天前'
}else if(offSetSeconds<60*60*24*30*12){
return'2个月前'
}else {
return'8年前'
}
}
var str = friendlyDate( '1484286699422' ) // 1分钟前
var str2 = friendlyDate('1183941245793') //4天前