Hi! Please, could anyone help with my “possible” solution?..
It works for all results but in case of palindrome(“almostomla”); doesn’t work. It is a bit absurd because I checked the string backward and forward and I got different results, obviously. But when I compare them I never get false for it. Thanks.
function palindrome(str) {
var newStr = str.toLowerCase().match(/[a-z0-9]/g).join("");
var text = "";
var text2 = "";
for(var i = 0; i <= newStr.length; i++){
text += newStr[i];
for(var j = newStr.length-1; j >= 0; j--){
text2 += newStr[j];
if(text === text2){
return true;
}
return false;
}
}
}
palindrome("almostomla");
Hint: do this again with a different variable, but use the .reverse() method before joining it again. I think you’ll find the resulting solution a lot simpler
I was checking my solution and finally I got it… I didn’t need a second for loop, only one and I had to compare with the first str…
I tried to delete my post when i realised my mistake. Many thanks for your help
New solution:
function palindrome(str) {
var newStr = str.replace(/[\W_]/g, "").toLowerCase();
var text = "";
for(var i = newStr.length-1; i >= 0; i--){
text += newStr[i];
if(text === newStr){
return true;
}
}
return false;
}
palindrome("My age is 0, 0 si ega ym.");