I am trying to work on various online exercises and I do not understand why my comparison of two arrays is not working. When I step through the Chrome debugger, I can see that the two arrays are equal.
The following is my code - given the string 'babbad':
const longestPalondrome = (str) => {
if (str.length < 1) return -1;
let tempStr,
orgStrSlice = [];
let tempArr = str.split('');
let lngstPlndrm = 0;
for (let i = 0; i < tempArr.length; i++) {
tempStr = tempArr.slice(0, i + 1);
orgStrSlice = [];
tempStr.map((elem) => {
orgStrSlice.push(elem);
});
// find palondrome
if (orgStrSlice == tempStr.reverse().join('')) {
if (tempStr.length >= lngstPlndrm || lngstPlndrm === 0) {
lngstPlndrm = tempStr.length;
}
} else {
console.log('not palondrome');
}
}
return lngstPlndrm;
};
The following line is where I am not seeing why this is not working:
orgStrSlice == tempStr.reverse().join('')
You can see by the following screen shot that the two arrays are equal 'b','a','b', which should return true, followed by going into the conditional to set lngstPlndrm to the length of the string giving us the longest palindrome for this iteration of the loop, but yet it jumps straight to the else conditional. Whereas, in the first iteration, it found 'b' was equal to 'b'
What am I doing wrong. As usual, thanks in advance
