Palindrome Checker-"almostomla"

Palindrome Checker-"almostomla"
0.0 0

#1

Tell us what’s happening:

My code works for all except “almostomla”.I’m not sure why.I’m trying to compare each element of both the arrays.If we consider the word “almostomla”,str1[4] = “s” and str2[4] = “t”,str1 in not equal to str2, hence the outcome should be “false” but my code’s output is “true”.
What is that I’m doing wrong?
Your code so far


function palindrome(str) {
  // Good luck!
  var str1=str.toLowerCase().replace(/\s/g, '').replace(/[^a-zA-Z 0-9]/gi,"").split("");
  var str2=str.toLowerCase().replace(/\s/g, '').replace(/[^a-zA-Z 0-9]/gi,"").split("").reverse();

for(var i =0;i<str.length;i++){
  if(str1[i] === str2[i]){
    return true;
  }
  return false;
}
}
 
 
palindrome("eye");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker


#2

Hiya! Couple thoughts:

  1. Have you tried using console.log() to verify the contents of str1 and str2 after you build them? That might be a good place to start by making sure that they’re built correctly. You can view the console.log outputs in the FCC editor as indicated at the bottom of the page.
  var str1=str.toLowerCase().replace(/\s/g, '').replace(/[^a-zA-Z 0-9]/gi,"").split("");
  var str2=str.toLowerCase().replace(/\s/g, '').replace(/[^a-zA-Z 0-9]/gi,"").split("").reverse();

  console.log(str1);
  console.log(str2);
  1. Try stepping through your for loop in your head. What happens when the loop evaluates the first character in each of the arrays? Is it working how you imagined it? You could also use console.log() statements inside of the loop to examine the compared characters each time the loop executes.

I hope this helps - feel free to reply if you have any other questions!
Evan


#4

Thank you for replying.
I’m checking my code simultaneously on visual studio code and it shows the output “true”.
Here’s my code:-
var str = “almostomla”;
str1 = str.toLowerCase().replace(/\s/g, ‘’).replace(/[^a-zA-Z 0-9]/gi,"").split("");
var str2=str.toLowerCase().replace(/\s/g, ‘’).replace(/[^a-zA-Z 0-9]/gi,"").split("").reverse();
console.log(str1);
console.log(str2);

function palen(strr){
for (var i =0;i<str.length;i++)
if(str1[i] === str2[i]){
return console.log(true);
}
return console.log(false);
}
palen(“almostomla”);


#5

Your program is working as expected based on what you’re doing in your for loop. In order to get the correct output, you’ll need to re-examine how your for-loop is verifying that the strings are the same. Again, I think you can make use of console.log() statements to troubleshoot the bug.


#6

I tried almost everything.I checked my o/p via console log statements ,also I tried using this syntax for the word “almostomla” and its shows false and that’s what I want but it messes with the other words.(I used “every()” method in the for loop to compare each element and it worked but only for this single word.)
function palen(str){
str1 = str.toLowerCase().replace(/\s/g, ‘’).replace(/[^a-zA-Z 0-9]/gi,"").split("");
var str2=str.toLowerCase().replace(/\s/g, ‘’).replace(/[^a-zA-Z 0-9]/gi,"").split("").reverse();
console.log(str1);
console.log(str2);

for (var i =0;i<str.length;i++)
if(str1[i].every === str2[i]){
    return console.log(true);
}
return console.log(false);

}
palen(“eebgee”);


#7

I believe that I made the same error that you are now making.

The problem:
When you say ‘return’ it quits the entire function, and outputs that answer - therefore, you are only checking whether the last character equals the first character.

The fix:
You need to have some flag variable that starts as true, and then, as you go through the different letters of the palindrome, if any of them don’t match, set the flag to false. At then end, you can then return the flag.


#8

I got it,all I had to do was add ‘join("")’ at the end so that the arrays become string again and then I can compare them directly without using for loop.Thanks!


#9

The other thing you could have done was to say if(str1 === str2)