inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[인프런 워밍업 스터디 클럽 2기 FE] 2주차 과제 - 비밀번호 생성 앱

김예지
0

 image비밀번호 생성 앱 만들기

GitHub : 06-password-generation

 

개요

 

필요한 기능

 

구현하기

//문자 범위
const charSets = {
  numbers: '0123456789',
  small: 'abcdefghijklmnopqrstuvwxyz',
  capital: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  symbols: '@!#$&%',
};

//사용자 체크박스 설정 
function getOptions() {
    return {
        numbers: document.getElementById("numbers").checked,
        small: document.getElementById("small").checked,
        capital: document.getElementById("capital").checked,
        symbols: document.getElementById("symbols").checked,
    };
}
createButton.addEventListener("click", () => {
    const length = parseInt(inputLength.value, 10); 
    const options = getOptions(); 

    /*비밀번호 생성 최소 조건 생략 */

    //Generator 사용할 때 
    const generatedPassword = generatePassword(length, options);

    //Factory Pattern 사용할 때
    const generator = PasswordFactory.createPasswordGenerator(options); 
    const generatedPassword = generator.generate(length);

    passwordElement.textContent = generatedPassword;
});

 사용자가 선택한 비밀번호 길이와 옵션을 전달받아 비밀번호를 생성한다. 위의 설정들과 shufflePassword(password) 비밀번호 셔플 함수, copyToClipboard(text) 생성된 비밀번호 복사 함수는 같고 비밀번호 생성 과정만 다르다.

 

Generator 사용

function generatePassword(length, options) {
    const generator = passwordGenerator(options);
    let password = "";

    // 선택된 옵션 배열 추가
    const selectedSets = [];
    if (options.numbers) selectedSets.push(charSets.numbers);
    if (options.small) selectedSets.push(charSets.small);
    if (options.capital) selectedSets.push(charSets.capital);
    if (options.symbols) selectedSets.push(charSets.symbols);

    // 각 문자 집합에서 하나씩 선택하여 추가 
    selectedSets.forEach((set) => {
        password += set.charAt(Math.floor(Math.random() * set.length));
        console.log(password);
    });

    // 나머지 자리에 대해 랜덤 문자 추가
    for (let i = password.length; i < length; i++) {
        password += generator.next().value;
    }

    // 비밀번호를 섞어서 반환
    return shufflePassword(password);
}

 generatePassword(length, options)함수는 비밀번호를 생성하고 길이를 관리합니다.

function* passwordGenerator(options) {
    const selectedSets = [];

    if (options.numbers) selectedSets.push(charSets.numbers);
    if (options.small) selectedSets.push(charSets.small);
    if (options.capital) selectedSets.push(charSets.capital);
    if (options.symbols) selectedSets.push(charSets.symbols);

    while (true) {
        const randomSet = selectedSets[Math.floor(Math.random() * selectedSets.length)];
        yield randomSet.charAt(Math.floor(Math.random() * randomSet.length));
    }
}

 passwordGenerator(options) 함수는 비밀번호를 생성합니다.

 

 

Factory Pattern 사용

// 비밀번호 생성기 클래스
class PasswordGenerator {
    constructor(options) {
        this.options = options;
        this.selectedSets = [];

        if (options.numbers) this.selectedSets.push(charSets.numbers);
        if (options.small) this.selectedSets.push(charSets.small);
        if (options.capital) this.selectedSets.push(charSets.capital);
        if (options.symbols) this.selectedSets.push(charSets.symbols);
    }

    // 비밀번호 생성
    generate(length) {
        let password = "";

        this.selectedSets.forEach((set) => {
            password += set.charAt(Math.floor(Math.random() * set.length));
        });

        while (password.length < length) {
            const randomSet = this.selectedSets[Math.floor(Math.random() * this.selectedSets.length)];
            password += randomSet.charAt(Math.floor(Math.random() * randomSet.length));
        }

        return shufflePassword(password);
    }
}


class PasswordFactory {
    static createPasswordGenerator(options) {
        return new PasswordGenerator(options);
    }
}

PasswordFactory.createPasswordGenerator(options) 를 호출하여 PasswordGenerator 인스턴스를 생성한 후, 인스턴스의 generate(length) 메서드를 통해 비밀번호를 생성한다.

 

 배운 걸 사용해보고자 Generator 함수와 클래스를 썼다.

 

 

 

워밍업클럽

답변 0