-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterview-question.js
More file actions
37 lines (28 loc) · 1.14 KB
/
interview-question.js
File metadata and controls
37 lines (28 loc) · 1.14 KB
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
// You receive the name of a city as a string, and you need to
// return a string that shows how many times each letter shows
// up in the string by using an asterisk (*).
// For example:
// "Chicago" --> "c:**,h:*,i:*,a:*,g:*,o:*"
// As you can see, the letter c is shown only once, but with 2
// asterisks.
// The return string should include only the letters (not the
// dashes, spaces, apostrophes, etc). There should be no spaces
// in the output, and the different letters are separated by a
// comma (,) as seen in the example above.
// Note that the return string must list the letters in order
// of their first appearence in the original string.
// More examples:
// "Bangkok" --> "b:*,a:*,n:*,g:*,k:**,o:*"
// "Las Vegas" --> "l:*,a:**,s:**,v:*,e:*,g:*"
function getStrings(city){
return JSON.stringify(city.toLowerCase().replace(/\s/g, '').split('').reduce((pre, cur)=>{
if(pre.hasOwnProperty(cur)){
pre[cur] += '*';
} else if(cur){
pre[cur] = "*";
}
return pre;
}, {})).replace(/\"/g, "").slice(1,-1);
}
const result = getStrings("Las Vegas");
console.log(result);