Leetcode - 1768. Merge Strings Alternately

Rakesh Reddy Peddamallu - Sep 9 - - Dev Community

Heres , the solution try to understand the code 🙃

/**
 * @param {string} word1
 * @param {string} word2
 * @return {string}
 */
var mergeAlternately = function(word1, word2) {

    const res = [];//final result array

    //looping sufficient for every char in both words 

    for(let i = 0 ;i < word1.length + word2.length ;i++){

        //pushing chars in word1
        if(i<word1.length){
            res.push(word1[i])
        }

        //pushing chars in word1
          if(i<word2.length){
            res.push(word2[i])
        }
    }
    return res.join('')
};
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player