-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone.js
79 lines (61 loc) · 1.96 KB
/
phone.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
76
77
78
79
// показывает все возможные варианты американских буквенных телефонных номеров
// например, 1-800-THIS это 1-800-TGGQ или 1-800-UGIQ, или 1-800-VIHS
var phone = '1-800-THIS';
var keymap = {
'0': [],
'1': [],
'2': 'ABC',
'3': 'DEF',
'4': 'GHI',
'5': 'JKL',
'6': 'MNO',
'7': 'PQRS',
'8': 'TUV',
'9': 'WXYZ',
'-' : '-'
};
// номер типа 1-800-ABC-EFGH, без ограничений длины
var num_part = phone.toUpperCase().match(/^1\-800\-([A-Z-]+)$/);
if (num_part === null) {
console.log('bad number');
process.exit();
}
// преобразуем ABC-EFGH в цифры, которые нужно набрать и сразу подсчитаем количество
// номеров, которые будут в результате
var digits = [];
var letters = num_part[1].split('');
var num_count = 1;
for (var k in letters) {
for (var j in keymap) {
if (keymap[j].indexOf(letters[k]) > -1) {
digits.push(j);
num_count *= keymap[j].length;
break;
}
}
}
var all_numbers_list = [];
var cur_count = 1;
var limit = 1;
var cur_letter = 0;
// список будет в массиве, добавим сразу 1-800- к результатам
for (k = 0; k < num_count; k++) {
all_numbers_list[k] = '1-800-';
}
// проходим по всем цифрам, которые надо набрать
// и добавляем к результату необходимое количество букв
// AD => 'A A A B B B C C C' на первом шаге
// и 'AD AE AF BD BE BF CD CE CF' на втором
for (var i in digits) {
cur_count = cur_count * keymap[digits[i]].length;
limit = num_count / cur_count;
k = limit;
for (j = 0; j < num_count; j++) {
all_numbers_list[j] += keymap[digits[i]][cur_letter];
if (!--k) {
k = limit;
cur_letter = (cur_letter + 1) % keymap[digits[i]].length;
}
}
}
console.log(all_numbers_list);