The idea is to write a function that takes two strings and returns a new string that repeats in the previous two: examples:
'ABBA' & 'AOHB' => 'AB'
'cohs' & 'ohba' => 'oh'
A brute force solution would be nested for loops like so:
const x = 'ABCD'
const y = 'AHOB'
function subStr(str1, str2) {
let final = ''
for (let i = 0; i < str1.length; i++) {
for (let j = 0; j < str2.length; j++) {
if (str1[i] === str2[j]) {
final += str1[i]
}
}
}
return final
}
console.log(subStr(x, y)) // => AB
subStr
function returns"ABBA"
on the input("ABBA", "AOHB")
, which isn't the output you specified ("AB"
) \$\endgroup\$