Skip to content

Instantly share code, notes, and snippets.

@vhxubo
Last active December 18, 2020 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vhxubo/7cc8385f39fdf0d9dd7205953e85baee to your computer and use it in GitHub Desktop.
Save vhxubo/7cc8385f39fdf0d9dd7205953e85baee to your computer and use it in GitHub Desktop.
生成随机学号
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