-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneNumber.js
More file actions
35 lines (29 loc) · 834 Bytes
/
PhoneNumber.js
File metadata and controls
35 lines (29 loc) · 834 Bytes
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
function getPhoneNumber(s) {
// Write your code here
const getPhoneMap = {
zero: '0',
one: '1',
two: '2',
three: '3',
four: '4',
five: '5',
six: '6',
seven: '7',
eight: '8',
nine: '9',
};
const words = s.split(' ');
let result = '';
for (let word of words){
if (word === 'double' || words === 'triple'){
const nextWord = words[words.indexOf(word) + 1];
const digit = getPhoneMap[nextWord[0]];
const repeatCount = word === 'double' ? 2 : 3;
result += digit.repeat(repeatCount);
} else {
result += getPhoneMap[word];
}
}
return result;
}
console.log(getPhoneNumber("two one nine six eight one six four six zero"));