Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

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

๐Ÿ”ฅ Algorithm/Programmers

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

Lennon 2021. 11. 5. 16:04
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/72410?language=javascript 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

์นด์นด์˜ค์— ์ž…์‚ฌํ•œ ์‹ ์ž… ๊ฐœ๋ฐœ์ž ๋„ค์˜ค๋Š” "์นด์นด์˜ค๊ณ„์ •๊ฐœ๋ฐœํŒ€"์— ๋ฐฐ์น˜๋˜์–ด, ์นด์นด์˜ค ์„œ๋น„์Šค์— ๊ฐ€์ž…ํ•˜๋Š” ์œ ์ €๋“ค์˜ ์•„์ด๋””๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—…๋ฌด๋ฅผ ๋‹ด๋‹นํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. "๋„ค์˜ค"์—๊ฒŒ ์ฃผ์–ด์ง„ ์ฒซ ์—…๋ฌด๋Š” ์ƒˆ๋กœ

programmers.co.kr

 

function solution(new_id) {
    
    let answer = new_id.toLowerCase();
    answer = answer.replace(/[^\w-._]+/gi,'')
    .replace(/\.\.+/g,'.')
    .replace(/\.$/g,'')
    .replace(/^\./g,'');
    
    if(answer[0] === ".") answer.splice(0,1);
    if(answer.length === 0) answer += 'a';
    if(answer.length > 15) answer = answer.slice(0,15);
    else if(answer.length <= 2) while(answer.length < 3) answer += answer[answer.length - 1];
    
    answer = answer.replace(/\.$/g,'');
    
    return answer;
}

 

๋ฌธ์ œ์—์„œ ์•Œ๋ ค์ฃผ๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.

 

์ฒ˜์Œ์—” 2๋ฒˆ์กฐ๊ฑด์„ ํ•ด๊ฒฐํ•˜๊ธฐ์œ„ํ•ด abcdef... 12345...-,_,. ๋“ฑ์„ ์„ ์–ธํ•ด์„œ for๋ฌธ์„ ๋Œ๋ ธ๋Š”๋ฐ ์ •๊ทœ์‹์œผ๋กœ ํ•˜๋ฉด ๋” ๊น”๋”ํ•  ๊ฒƒ ๊ฐ™์•„ ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

๋‚˜๋จธ์ง„ ๊ทธ๋ƒฅ ๋‹จ๊ณ„๋ณ„๋กœ ํ•˜๋ผ๋Š”๋Œ€๋กœ ํ•˜๋ฉด ๋œ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•
Comments