-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-strings-alternately.ts
46 lines (38 loc) · 1.06 KB
/
merge-strings-alternately.ts
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
function mergeAlternately(word1: string, word2: string): string {
let result = "";
let minLength = Math.min(word1.length, word2.length);
for (let i = 0; i < minLength; i++) {
result += word1.charAt(i) + word2.charAt(i);
}
if (word1.length > minLength) {
result += word1.slice(minLength);
}
if (word2.length > minLength) {
result += word2.slice(minLength);
}
return result;
};
/**
* First submttion
*
* submitted with below the avarge.
* i think i can did that all in map func.
*/
// function mergeAlternately(word1: string, word2: string): string {
// let chartArr1 = word1.split("");
// let chartArr2 = word2.split("");
// let result = "";
// for (let i = 0; i < chartArr1.length; i++) {
// if (chartArr2[i] === undefined) {
// result += chartArr1[i];
// } else {
// result += chartArr1[i] + chartArr2[i];
// }
// }
// if (chartArr1.length < chartArr2.length) {
// for (let i = chartArr1.length; i < chartArr2.length; i++) {
// result += chartArr2[i];
// }
// }
// return result;
// }