var str1 = 'я первое, а я',
result = '',
str2 = 'а я второе';
result = method(str1, str2)
//желанный результат:
result -> 'я первое, а я второе'
var str1 = 'как я ', str2 = 'яма';
var concat_with_replace = function(str1, str2) {
var str1_words = str1.split(' '),
str2_words = str2.split(' '),
count = 0, last_word = -1, new_word = -1;
for(var i = 0; i < str2_words.length; i++) {
for(var j = str1_words.length-1; j >= 0; j--) {
if(str1_words[j] === str2_words[i] || str1_words[j] == "" || str2_words[i] === "") {
new_word = j;
break;
}
}
if(new_word !== last_word + 1 && last_word !== -1 || new_word === -1) {
break;
} else {
new_word = -1;
last_word = new_word;
count++;
}
}
var result = str1_words.splice(0 , str1_words.length-count).concat(str2_words);
for(var i = 0 ; i < result.length; i++) {
if(result[i] == "") {
result.splice(i, 1);
i--;
}
}
return result.join(' ');
}
console.log(concat_with_replace(str1, str2));
function merge_string(str1, str2){
var str3 = str2.slice(0, str1.length), str4;
for (var i=str3.length; i>0; i--){
str4 = str1.slice(-i);
if (str4.indexOf(str2.slice(0, str4.length)) == 0){
return str1.slice(0, -i) + str2
}
}
}