Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Easy/1768. Merge Strings Alternately/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function mergeAlternately(word1, word2) {
// To store the final string
let result = "";
// For every index in the strings
for (let i = 0; i < word1.length || i < word2.length; i++) {

// First choose the ith character of the
// first string if it exists
if (i < word1.length)
result += word1.charAt(i);

// second string if it exists
if (i < word2.length)
result += word2.charAt(i);

}

return result;
}

// Driver code
let word1 = "abc";
let word2 = "pqr";
console.log(mergeAlternately(word1, word2));
//This code is Contributed by chinmaya121221