進階六 Math 數(shù)組 Date

Math任務

1.寫一個函數(shù),返回從min到max之間的 隨機整數(shù),包括min不包括max

  var min = 7
  var max = 11
  function getRandom(min,max){
      return Math.floor(Math.random()*(max-min)+ min);
  }
  var a = getRandom(min,max)
  console.log(a)

2. 寫一個函數(shù),返回從min都max之間的 隨機整數(shù),包括min包括max

  var min = 4
  var max = 7
  function getRandom(min,max){
    return Math.floor(Math.random()*(max-min+1)+ min);
    }
  var a = getRandom(min,max)
  console.log(a)

3. 寫一個函數(shù),生成一個長度為 n 的隨機字符串,字符串字符的取值范圍包括0到9,a到 z,A到Z。

    var dict =       "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU    VWXYZ"
var newstr = ''
function getRandStr(n){
for(var i = 1;i < n; i++){
    var a = Math.floor(Math.random()*62)
    newstr += dict[a]
}
  return newstr
}
var str = getRandStr(10); 
console.log(str)

4. 寫一個函數(shù),生成一個隨機 IP 地址,一個合法的 IP 地址為 0.0.0.0~255.255.255.255

function getRandIP(){
var ip = []
for(var i= 0;i < 4 ;i++){
    ip[i] = Math.floor(Math.random()*256)
}
return ip.join(".")
}
var ip = getRandIP()
console.log(ip)

5. 寫一個函數(shù),生成一個隨機顏色字符串,合法的顏色為#000000~ #ffffff

function getRandColor(){
  var dict = "0123456789abcdef"
var ip = "#"
for(var i= 0;i < 6 ;i++){
    ip += dict[Math.floor(Math.random()*16)]
}
return ip
}
var color = getRandColor()
console.log(color)

數(shù)組任務

1. 數(shù)組方法里push、pop、shift、unshift、join、splice分別是什么作用?用 splice函數(shù)分別實現(xiàn)push、pop、shift、unshift方法

  • push()方法用于將值添加進數(shù)組,返回數(shù)組長度
  • pop()方法刪除數(shù)組最后一個元素,返回被刪除的元素
  • shift()方法刪除數(shù)組第一個元素,返回被刪除的元素
  • unshift()方法在數(shù)組開頭添加一個或多個值,返回數(shù)組長度
  • join()方法以指定分隔符將數(shù)組的值鏈接成字符串,返回生成的新字符串,()內(nèi)接受指定分隔符參數(shù),若為空字符,則元素直接聯(lián)結(jié)成字符串
  • splice() 方法向/從數(shù)組中添加/刪除項目,然后返回被刪除的項目。
    arrayObject.splice(index,howmany,item1,.....,itemX)
    index 必需。整數(shù),規(guī)定添加/刪除項目的位置,使用負數(shù)可從數(shù)組結(jié)尾處規(guī)定位置。
    howmany 必需。要刪除的項目數(shù)量。如果設置為 0,則不會刪除項目。
    item1, ..., itemX 可選。向數(shù)組添加的新項目。
  • 用splice實現(xiàn)push
    arrayObject.splice(arrayObject.length, 0, 22)
  • 用splice實現(xiàn)pop
    arrayObject.splice(arrayObject.length, 1)
  • 用splice實現(xiàn)shift
    arrayObject.splice(0, 1)
  • 用splice實現(xiàn)unshift
    arrayObject.splice(0, 0,1)

2. 寫一個函數(shù),操作數(shù)組,數(shù)組中的每一項變?yōu)樵瓉淼钠椒剑谠瓟?shù)組上操作

function squareArr(arr){
var newarr = []
for(var i = 0;i < 3; i++){
newarr[i] = arr[i]*arr[i]
}
return newarr
}
var arr = [2, 4, 6]
squareArr(arr)
console.log(squareArr(arr))

3. 寫一個函數(shù),操作數(shù)組,返回一個新數(shù)組,新數(shù)組中只包含正數(shù),原數(shù)組不變

function filterPositive(arr){
function callback(element){
        return element > 0 && typeof element ==="number"
}
return arr.filter(callback)
}
var arr = [3, -1, 2, '饑人谷', true]
var newArr = filterPositive(arr)
console.log(newArr) //[3, 2]
console.log(arr) //[3, -1, 2, '饑人谷', true]

Date 任務

1. 寫一個函數(shù)getChIntv,獲取從當前時間到指定日期的間隔時間

function getChIntv(time){
    var newtime = new Date()
    var oldtime = new Date(time)
    var total = newtime - oldtime
    var day = Math.floor(total / (1000*60*60*24))
    var hour = Math.floor((total % (1000*60*60*24)) / (1000*60*60))
    var minute = Math.floor(((total % (1000*60*60*24)) % (1000*60*60)) / (1000*60))
    var chtime = ""
    return chtime = "距離"+ time + "還有"+day + "天" + hour + "小時"+ minute + "分" 
}
var str = getChIntv("2017-02-08");
console.log(str); // 距除夕還有 20 天 15 小時 20 分 10 秒

2. 把hh-mm-dd格式數(shù)字日期改成中文日期

  function getChsDate(str){
    var dateArr = str.split("-"),
 yearStr = dateArr[0],
 monthStr = dateArr[1],
 dayStr = dateArr[2],
 dict = "零,一,二,三,四,五,六,七,八,九,十,十一,十二,十三,十四,十五,十六,十七,十八,十九,二十,二十一,二十二,二十三,二十四,二十五,二十六,二十七,二十八,二十九,三十,三十一";
  dict = dict.split(",")
  var chYearStr = dict[parseInt(yearStr[0])] + dict[parseInt(yearStr[1])] +         dict[parseInt(yearStr[2])] + dict[parseInt(yearStr[3])] + "年",
  chMonthStr = dict[parseInt(monthStr)] + "月",
  chDayStr = dict[parseInt(dayStr)] + "日";
  return chYearStr + chMonthStr + chDayStr
}
var str = getChsDate('2015-01-08');
console.log(str); // 二零一五年一月八日

3.寫一個函數(shù),參數(shù)為時間對象毫秒數(shù)的字符串格式,返回值為字符串。假設參數(shù)為時間對象毫秒數(shù)t,根據(jù)t的時間分別返回如下字符串:

  • 剛剛( t 距當前時間不到1分鐘時間間隔)
  • 3分鐘前 (t距當前時間大于等于1分鐘,小于1小時)
  • 8小時前 (t 距離當前時間大于等于1小時,小于24小時)
  • 3天前 (t 距離當前時間大于等于24小時,小于30天)
  • 2個月前 (t 距離當前時間大于等于30天小于12個月)
  • 8年前 (t 距離當前時間大于等于12個月)

        var total = Date.now() - parseInt(time)
    if(total < 600000){
    return "剛剛"
}
if(total >=60000 && total < 60000*60){
    return "3分鐘前"
}
if(total >=60000 && total < 60000*60*24){
    return "8小時前"
}
if(total >=60000*60*24 && total < 60000*60*24*30){
    return "3天前"
}
if(total >=60000*60*24*30 && total < 60000*60*24*30*12){
    return "2個月前"
}
if(total >=60000*60*24*30*12){
    return "8年前 "
}
  }
  var str = friendlyDate( '1495192760000' ) 
  var str2 = friendlyDate('1495102762685') 
  console.log(str)
  console.log(str2)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容