-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsomehting.js
More file actions
58 lines (41 loc) · 1.48 KB
/
somehting.js
File metadata and controls
58 lines (41 loc) · 1.48 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function solution(s1, s2) {
let firstObj = count(s1)
let secontObj = count(s2)
const merged = (s1, s2) => {
const newWord = new Array();
let s1I = 0;
let s2I = 0;
const nS1 = s1.split('')
const nS2 = s2.split('')
const length = s1.length + s2.length
for(let i = 0; i < length; i++) {
const firstletter = nS1[s1I]
const secondletter = nS2[s2I]
if((firstObj[firstletter] < secontObj[secondletter]) || !secondletter) {
newWord.push(firstletter)
s1I ++
} else if((firstObj[firstletter] > secontObj[secondletter]) || firstletter == undefined) {
newWord.push(secondletter)
s2I ++
} else if(firstObj[firstletter] == secontObj[secondletter]) {
if(firstletter > secondletter) {
newWord.push(secondletter)
s2I++
} else {
newWord.push(firstletter)
s1I++
}
}
}
return newWord.join('')
}
console.log(firstObj)
console.log(secontObj)
return merged(s1, s2)
}
function count(string) {
return string.split('').reduce((col, cur) => {
col[cur] = col[cur]? col[cur] += 1 : col[cur] = 1
return col
}, {})
}