반응형

 

출처: https://programmers.co.kr/learn/courses/30/lessons/72410

 

 

풀이가 굉장히 마음에 안들지만

 

** Javascript

 

function solution(new_id) {
    let allowed = "abcdefghijklmnopqrstuvwxyz-_.".split("");
    let charArr = new_id.toLowerCase().split("");
    let answer = charArr
        .filter(char => allowed.includes(char) || (!isNaN(char) && !isNaN(parseFloat(char))))
        .join("");
    while (answer.match(/\.\./)) {
        answer = answer.replace(/\.\./g, ".");
    }
    while (answer.startsWith(".")) {
       answer = answer.substr(1, answer.length - 1);
    }
    while (answer.endsWith(".")) {
        answer = answer.substr(0, answer.length - 1);
    }
    if (answer.length >= 16) answer = answer.slice(0, 15);
    while (answer.endsWith(".")) {
        answer = answer.substr(0, answer.length - 1);
    }
    if (answer === "") answer = "a";
    if (answer.length <= 2) {
        for (var i = answer.length; i < 3 ; i++) {
            answer += answer[answer.length - 1];
        }
    }    
    return answer;
}
반응형

+ Recent posts