DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ (Javascript) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ (Javascript)

gimewn 2022. 10. 26. 00:56

link ๐Ÿ”— https://school.programmers.co.kr/learn/courses/30/lessons/72410

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

โœ๏ธ ๋ฌธ์ œ์—์„œ ์ œ์‹œ๋˜๋Š” ๋‹จ๊ณ„์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌ๋งŒ ์ž˜ ํ•ด์ฃผ๋ฉด ๋ณ„ ๋ฌธ์ œ ์—†์ด ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

โœ๏ธ ๊ทธ๋Ÿฐ๋ฐ ๊ณ„์† ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 3๋ฒˆ์—์„œ ์‹คํŒจํ–ˆ๋‹ค...๐Ÿ˜ญ

โœ๏ธ ๋””๋ฒ„๊น…์˜ ๋””๋ฒ„๊น…์˜ ๋””๋ฒ„๊น…์„ ๊ฑฐ์น˜๋‹ค ์ด์œ ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋Š”๋ฐ, ์ธ๋ฑ์Šค 0๊ณผ -1์— .์ด ์™”์„ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ค„ ๋•Œ if์™€ else if๋กœ ์ฒ˜๋ฆฌํ•ด๋ฒ„๋ ค์„œ ์ธ๋ฑ์Šค 0๋งŒ .์ธ ๊ฒฝ์šฐ์™€ ์ธ๋ฑ์Šค -1๋งŒ .์ธ ๊ฒฝ์šฐ๋Š” ์ฒ˜๋ฆฌํ•˜์ง€๋งŒ, ์ธ๋ฑ์Šค 0๊ณผ -1 ๋ชจ๋‘ .์ธ ๊ฒฝ์šฐ ์ธ๋ฑ์Šค 0๋งŒ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด์—ˆ๋‹ค!

โœ๏ธ ์‹ค์ˆ˜ ์กฐ์‹ฌ... ๋ฐ”๋ณด ๊ฐ™์€ ์‹ค์ˆ˜๋กœ ์‹œ๊ฐ„์„ ๋งŽ์ด ๋‚ ๋ ธ์ง€๋งŒ ๊ทธ๋ž˜๋„ ์ด์œ ๋ฅผ ์ฐพ์•„์„œ ํ•ด๊ฒฐํ•ด์„œ ๊ธฐ๋ถ„์ด ์ข‹๋‹ค ใ…Žใ…Ž

 

function solution(new_id) {
    var answer = '';
    // 1. ์†Œ๋ฌธ์ž ๋ณ€ํ™˜
    new_id = new_id.toLowerCase().split("");
    // 2. ํฌํ•จ๋˜๋ฉด ์•ˆ ๋˜๋Š” ๋ฌธ์ž ์‚ญ์ œ
    const is_valid = new_id.filter((item) => {
        if((item >= "a" && item <= "z") || 
           (item >= "0" && item <= "9") ||
          (item === "-") || (item === ".") || (item === "_")){
            return item
        }
    })
    // 3. . ์—ฐ์† ์ฒ˜๋ฆฌ
    let delete_dot = "";
    for(let idx = 0; idx < is_valid.length-1; idx++){
        if(is_valid[idx] === "." && is_valid[idx+1] === "."){
            delete_dot += '';
        }else{
            delete_dot += is_valid[idx];
        }
    }
    // ๋งˆ์ง€๋ง‰ ๋ฌธ์ž ํฌํ•จ
    delete_dot += is_valid.at(-1);
    
    // 4. ์ฒ˜์Œ๊ณผ ๋์— ์žˆ๋Š” . ์ฒ˜๋ฆฌ
    let delete_dot_arr = delete_dot.split("");
    if(delete_dot_arr.at(0) === "."){
        delete_dot_arr = delete_dot_arr.slice(1)
    }
    if(delete_dot_arr.at(-1) === "."){
        delete_dot_arr = delete_dot_arr.slice(0, delete_dot_arr.length-1);
    }
    
    // 5. ๋นˆ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ
    if(delete_dot_arr.length === 0){
        delete_dot_arr = ["a"]
    }
    
    // 6. ๋ฌธ์ž์—ด ๊ธธ์ด ์ฒ˜๋ฆฌ
    if(delete_dot_arr.length > 15){
        delete_dot_arr = delete_dot_arr.slice(0, 15);
        if(delete_dot_arr.at(-1) === "."){
            delete_dot_arr = delete_dot_arr.slice(0, delete_dot_arr.length-1);
        }
    }
    if(delete_dot_arr.length < 3){
        while(delete_dot_arr.length < 3){
            delete_dot_arr.push(delete_dot_arr.at(-1));
        }
    }
    answer = delete_dot_arr.join("");
    return answer
}

 

โญ ์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ๋„ ํ’€์–ด๋ณด๊ธฐ!!