merge two strings with alternate characters javascript 1

merge two strings with alternate characters javascript

var m = (a, b) => a.length ? [a[0], ...m(b, a.slice(1))] : b;
var string1 = "SCE ESG!";
var string2 = "ERTMSAE";
var mix = m(string1, string2);

console.log(mix.join(''));

Here is what the above code is Doing:
1. We define a function called m that takes two strings as arguments.
2. We use the ternary operator to check if the first string is empty.
3. If it is, we return the second string.
4. If it isn’t, we return the first character of the first string, followed by the result of calling m with the second string and the rest of the first string.
5. We call m with the two strings and join the result.

Similar Posts