When I try and use this code it tells me:
“TypeError: together.reverse is not a function. (In ‘together.reverse()’, ‘together.reverse’ is undefined)”
I am obviously miss understanding something here, but even with research it’s just not clicking. Could someone explain why the code is wrong?
Here’s my code so far.
function palindrome(str) {
let newStr = str.toLowerCase().match(/[a-z0-9]/g);
let together = newStr.join('');
let backwards = together.reverse();
if (together === backwards) {
return true;
} else {
return false;
}
}
palindrome("eye");
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15
Oh my goodness thank you so much!
It still took a minute but I think I understand now.
.reverse() would only work on an array, so I needed to do that before .join(’’).
I ended up with this:
function palindrome(str) {
let newStr = str.toLowerCase().match(/[a-z0-9]/g);
let together = newStr.join(’’);
let flip = newStr.reverse();
let backwards = flip.join(’’);
if (together === backwards) {
return true;
} else {
return false;
}
}