-
-
Save vhxubo/7cc8385f39fdf0d9dd7205953e85baee to your computer and use it in GitHub Desktop.
生成随机学号
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const grade = [9, 10, 11, 12]; | |
const sequence = []; | |
for (let i = 0; i <= 30; i++) { | |
sequence.push(i); | |
} | |
const generate = (arr) => { | |
const obj = {}; | |
for (item of arr) | |
if (item < 100) { | |
const l = item % 10; | |
const hStr = Math.floor(item / 10).toString(); | |
if (!obj.hasOwnProperty(hStr)) { | |
obj[hStr] = []; | |
} | |
obj[hStr].push(l); | |
} | |
return obj; | |
}; | |
const Lobj = { grade: generate(grade), sequence: generate(sequence) }; | |
const getResult = (obj) => { | |
let keysArr = Object.keys(obj); | |
let result = {}; | |
result.high = keysArr[Math.floor(Math.random() * keysArr.length)]; | |
result.low = | |
obj[result.high][Math.floor(Math.random() * obj[result.high].length)]; | |
return result.high + result.low; | |
}; | |
console.log(getResult(Lobj.grade) + "-" + getResult(Lobj.sequence)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment