Skip to content

Commit d225d2b

Browse files
authored
Create 1678-merge-strings-alternately.js
1 parent 8d5dc32 commit d225d2b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

1678-merge-strings-alternately.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} word1
3+
* @param {string} word2
4+
* @return {string}
5+
*/
6+
var mergeAlternately = function(word1, word2) {
7+
let res = '', mark = 0, i = 0, j = 0
8+
while(i < word1.length && j < word2.length) {
9+
if(mark === 0) {
10+
res += word1[i++]
11+
mark = 1
12+
} else {
13+
res += word2[j++]
14+
mark = 0
15+
}
16+
}
17+
while(i < word1.length) res += word1[i++]
18+
while(j < word2.length) res += word2[j++]
19+
20+
return res
21+
};

0 commit comments

Comments
 (0)