تمرين إعادة البناء
Password Generator
أعد كتابة مولّد كلمات مرور TypeScript بإعدادات typed.
typescript
~10 دقيقة
مبتدئ
أعد بناء الكود
Rebuild
هذا هو الكود. اكتبه بنفسك.
الكود المرجعي
type PasswordOptions = {
length: number;
includeSymbols: boolean;
includeNumbers: boolean;
};
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numbers = "0123456789";
const symbols = "!@#$%^&*";
function buildAlphabet(options: PasswordOptions): string {
let alphabet = letters;
if (options.includeNumbers) {
alphabet += numbers;
}
if (options.includeSymbols) {
alphabet += symbols;
}
return alphabet;
}
function generatePassword(options: PasswordOptions): string {
const alphabet = buildAlphabet(options);
let password = "";
for (let index = 0; index < options.length; index += 1) {
const randomIndex = Math.floor(Math.random() * alphabet.length);
password += alphabet[randomIndex];
}
return password;
}
console.log(generatePassword({ length: 16, includeSymbols: true, includeNumbers: true }));اكتب هنا