'A new string of two other substrings

Well, the essence of the task. There are 3 strings (str1, str2, str3). You need to write a function that will return str1 but consisting of srt2 and str3. One of them should be under toUpperCase().

Example:

str1 = stopam

str2 = sam
str3 = top

return => StopAM

I wanted to do it sequentially use (.push) to a new array to then convert to a string but it didn't work out, but for some reason it doesn't work, tell me how to fix it. I apologize for my English.

var resMas = []

var mas1 = ["s","t","o", "p", "g", "a", "m", "e"],
    mas2 = ["s", "a", "m"],
    mas3 = ["t", "o", "p", "g", "e"]

function revString(mas1, mas2, mas3){
  for(let i = 0; i < mas1.length; i++){
    if (mas2.indexOf(mas1[i]) != -1){
      resMas.push(mas2[i])
      console.log(resMas)
    } else if (mas3.indexOf(mas1[i]) != -1){
      resMas.push(mas3[i])
      console.log(resMas)
    }
  }
  return resMas;
}

console.log(revString(mas1, mas2, mas3)) //StopgAMe


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source