-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
39 lines (35 loc) · 834 Bytes
/
solution.js
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
/**
* @param {string} word1
* @param {string} word2
* @return {string}
*/
var mergeAlternately = function (word1, word2) {
let str = "";
word1 = word1.split(""), word2 = word2.split("");
for (let i = 0; i < word1.length; i++) {
str += word1[i];
if (word2.length > 0) str += word2.shift();
}
str += word2.join("");
return str;
};
// var mergeAlternately = function (word1, word2) {
// let str = "";
// word1 = word1.split(""), word2 = word2.split("");
// while (word1.length > 0 || word2.length > 0) {
// let curr1 = "", curr2 = "";
// console.log(word1)
// console.log(word2)
// if (word1.length > 0) {
// curr1 = word1.shift();
// }
//
// if (word2.length > 0) {
// curr2 = word2.shift();
// }
//
// str += curr1 + curr2;
// }
//
// return str;
// };