Skip to content

Commit a2dcdfb

Browse files
committed
mergestringsalternately
1 parent 3296884 commit a2dcdfb

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1768-merge-strings-alternately.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} word1
3+
* @param {string} word2
4+
* @return {string}
5+
*/
6+
var mergeAlternately = function(word1, word2) {
7+
let w1len = word1.length;
8+
let w2len = word2.length;
9+
10+
let result = [];
11+
12+
for (let i = 0; i < Math.max(w1len, w2len); i++) {
13+
if (i < w1len) {
14+
result.push(word1[i]);
15+
}
16+
17+
if (i < w2len) {
18+
result.push(word2[i]);
19+
}
20+
}
21+
22+
return result.join("");
23+
};

0 commit comments

Comments
 (0)