Files
exercise-typescript/typescript/robot-name/robot-name.ts
2022-11-06 14:58:29 +01:00

48 lines
1.0 KiB
TypeScript

const allNames: string[] = [];
for (let c1 = 0; c1 < 26; c1++) {
const letter1 = String.fromCharCode(c1 + 65);
for (let c2 = 0; c2 < 26; c2++) {
const letter2 = String.fromCharCode(c2 + 65);
for (let n1 = 0; n1 < 10; n1++) {
const number1 = String(n1);
for (let n2 = 0; n2 < 10; n2++) {
const number2 = String(n2);
for (let n3 = 0; n3 < 10; n3++) {
const number3 = String(n3);
allNames.push(`${letter2}${letter1}${number1}${number2}${number3}`);
}
}
}
}
}
let possibleNames = [...allNames];
const generateName = (): string => {
const index = Math.floor(Math.random() * possibleNames.length);
const name = possibleNames[index];
possibleNames.splice(index, 1);
return name;
};
export class Robot {
private _name = "";
constructor() {
this.resetName();
}
public get name(): string {
return this._name;
}
public resetName(): void {
this._name = generateName();
}
public static releaseNames(): void {
possibleNames = [...allNames];
}
}