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
32
33
34
35
36
37
38
39
40
41
42
| function solution(new_id) {
//1단계 소문자로 치환
let lowId = new_id.toLowerCase();
let arrId = lowId.split("");
//2단계 문자열 제거
let correctType = "abcdefghijklnmoptqrsvwxyz0123456789-_.";
let filtered2ArrId = arrId.filter((e) => correctType.includes(e));
//3단계 마침표 2번이상 제거
let DotfilteredArrId = filtered2ArrId.filter((e,i) => e !== filtered2ArrId[i+1])
//4단계 처음이나 끝에 있을떄 제거
if(DotfilteredArrId[0] === ".") {DotfilteredArrId.shift()}
if(DotfilteredArrId[DotfilteredArrId.length - 1] === ".") {DotfilteredArrId.pop()}
//5단계 빈문자열이라면 "a"대입
let emptyToAArray = DotfilteredArrId.map((e) => {
if(e === " ") {
return "a"
}
return e
})
if(emptyToAArray.length === 0) {
emptyToAArray = ["a"]
}
//6단계 16이상 이면? 15까지 자르고 마지막에 .이 있으면 그걸 제거한다.
let result = emptyToAArray
if(result.length > 15) {
result = result.slice(0, 15)
if(result[result.length - 1] === ".") {
result.pop()
}
}
//7단계 2 이하라면 마지막문자의 길이가 3이 될때까지 반복한다.
if(result.length <= 2) {
let i = 0
while(result.length < 3) {
result.push(result[result.length - 1])
i++
}
}
return result.join("")
}
|