-
Notifications
You must be signed in to change notification settings - Fork 0
/
1694. Reformat Phone Number.js
75 lines (75 loc) · 1.73 KB
/
1694. Reformat Phone Number.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var reformatNumber = function (number) {
numArray = [...number];
newArray = [];
for (i = 0; i < numArray.length; i++) {
if (numArray[i] === " " || numArray[i] === "-") {
continue;
} else {
newArray.push(numArray[i]);
}
}
console.log("numy", numArray);
console.log("nw", newArray);
ans = "";
function blockFormation(num, newArray) {
res = "";
for (i = 0; i < num; i++) {
digit = newArray.pop();
res = digit + res;
}
return res;
}
function dashing(stringy) {
return "-" + stringy;
}
n = newArray.length;
switch (n % 3) {
case 1:
ans = blockFormation(2, newArray);
if (newArray.length > 0) {
ans = dashing(ans);
}
ans = blockFormation(2, newArray) + ans;
if (newArray.length > 0) {
ans = dashing(ans);
}
while (newArray.length > 0) {
ans = blockFormation(3, newArray) + ans;
if (newArray.length > 0) {
ans = dashing(ans);
} else {
break;
}
}
console.log(newArray);
break;
case 2:
ans = blockFormation(2, newArray);
if (newArray.length > 0) {
ans = dashing(ans);
}
while (newArray.length > 0) {
ans = blockFormation(3, newArray) + ans;
if (newArray.length > 0) {
ans = dashing(ans);
} else {
break;
}
}
break;
case 0:
console.log(newArray);
while (newArray.length > 0) {
console.log("ans", ans);
ans = blockFormation(3, newArray) + ans;
if (newArray.length > 0) {
console.log("b4dashing", ans);
ans = dashing(ans);
} else {
break;
}
}
break;
}
return ans;
};