Why Array.push doesnt work inside Nested For Loop? But it works if I replace 2nd for Loop with forEach
var longestCommonPrefix = function (strs) {
if (strs.length === 1) {
return strs.join('')
}
let reference = strs[0].split('');
let answer = [];
let final = [];
for (let i = 1; i < strs.length; i++) {
let check = strs[i].split('')
for (let x = 0; x < reference.length; x++) {
if (reference[x] === check[x]) {
answer.push(check[x]) //WHY THIS WONT WORK?
} else return
}
reference = answer
}
console.log(answer)
};
longestCommonPrefix(["flower", "flow", "flight"]);