getRandom (x) {
let val = '';
if (x <= 10) {
val = Math.random().toString().slice(2, x + 2)
} else {
let mul = Math.floor(x / 10)
let surplur = x - 10 * mul
for (let i = 0; i < mul; i++) {
val += Math.random().toString().slice(2, 12)
}
surplur && (val = val + Math.random().toString().slice(2, surplur + 2))
}
return val
}
getRandom(5)
getRandom(10)
getRandom(15)
getRandom(20)
const array = Array(5).fill('')
array
const matrix = Array(5).fill(0).map(()=>Array(5).fill(0))
matrix
const array = [5,4,7,8,9,2]
array.reduce((a,b)=>a+b)
array.reduce((a,b)=>a>b?a:b)
array.reduce((a,b)=>a<b?a:b)
--排序字符串数组
const stringArr = ['Joe','Kapil','Steve','Musk']
stringArr.sort()
stringArr.reverse()
--排序数字数组
const array = [40,100,1,5,25,10]
array.sort((a,b)=>a-b)
array.sort((a,b)=>b-a)
--对象数组排序
const objectArr = [
{first_name:'Lazslo',last_name:'Jamf'},
{first_name:'Pig',last_name:'Bodine'},
{first_name:'Pirate',last_name:'Prentice'},
]
objectArr
0:{first_name:'Pig',last_name:'Bodine'}
1:{first_name:'Lazslo',last_name:'Jamf'}
2:{first_name:'Pirate',last_name:'Prentice'}
const array = [3,0,6,7,'',false]
array.filter(Boolean)
function doSomething(arg1){
arg1 = arg1 || 10
return arg1
}
let foo = 10
foo === 10 && doSomething()
foo === 5 || doSomething()
foo === 10 || doSomething()
function Fever(temp){
return temp > 97 ? 'Visit Doctor!' : temp < 97 ? 'Go Out and Play!!' : temp === 97 ? 'Take Some Rest!'
}
Fever(97)
Fever(100)
let string = 'kkkapiliekty'
const table = {}
for(let char of string){
table[char] = table[char] + 1 || 1
}
table
let a = 5
let b = 8
[a,b] = [b,a]
[a,b]
|