We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7316cce commit b008602Copy full SHA for b008602
solutions/1768.merge-strings-alternately.js
@@ -0,0 +1,42 @@
1
+/**
2
+ * @param {string} word1
3
+ * @param {string} word2
4
+ * @return {string}
5
+ */
6
+var mergeAlternately = function (word1, word2) {
7
+ const L1 = word1.length;
8
+ const L2 = word2.length;
9
+
10
+ if (L1 === 0 && L2 === 0) {
11
+ return word1;
12
+ } else {
13
+ if (L1 === 0) {
14
15
+ } else if (L2 === 0) {
16
+ return word2;
17
+ }
18
19
20
+ let out = new Array(L1 + L2);
21
+ let i = 0;
22
+ let upper = Math.min(L1, L2);
23
24
+ while (i < upper) {
25
+ out.push(word1[i]);
26
+ out.push(word2[i]);
27
28
+ i++;
29
30
31
+ if (L1 === L2) {
32
+ return out.join("");
33
34
35
+ if (i === L1) {
36
+ out.push(word2.slice(i));
37
38
+ out.push(word1.slice(i));
39
40
41
42
+};
0 commit comments