Palindrom check in Javascript not working

javascript

#1

I am trying to write afunction to check if a word or sentence is palindrome or not

it is some how not working or giving error

function palindrome(str) {
  // Good luck!
 // var n = str.toLowerCase();
  
  var len = str.length;
  var half = Math.floor(len / 2);

  
  for(var i= 0; i < half; i++){
    if(str.charAt[i] !== str[len - 1]) {
      return false;
    }else {
       return true;
  }
    
}
     
}
palindrome("eye");
palindrome("race car");
palindrome("not a palindrome");

appreciate assistance.


#2

why use charAt? you can simple do str[i] to get the letter at a certain index, this also seems to be the reason your code is not working


#3

How this should work:

for odd numbers of character. say eye?


#4

works fine. 3 / 1 = 1. then in the if statement (str[len -1] means 1 - 1, which is 0, which is perfect. works fine for eye


#5

You mean:
3 /2 = 1;

But Str[3-1] =2 ?


#6

oops, you are right, but it will still work.

e   y   e
0   1   2

so Str[3-1] =2 will be e, i will be zero so then we have str[0] (e) and str[2] (e), which are equal.


#7

I think you guys are wanting something like this?:

var str = prompt("Give us your strings. We will check if you are Palindrom Syndrom positive or not?").toUpperCase();
var z =[];

var alphatest = function(str)
{
    
    for (j=0; j<str.length; j++)
     {
        var code = str.charCodeAt(j);
         if ((code > 64 && code < 91) | (code > 96 && code < 123))
         
                 z.push(str[j]);
        
        
     }

};
alphatest(str);

var x = z.reverse();

if (x === z)
 console.log("You've got Palindrom syndrome!");
else
console.log("Negative");

#8

i don't understand, the purpose of this topic was to help id123 with his code, of course i could write a better solution, but that is not the point


#9

Of course. Oh.... I can't resist the fun of doing this. Especially I did not know the term Palindrom before.

I think the person was trying to get character code that is why id123 wrote charAt[i]. Anyway, never mind.


#10

if you want such challenge, i highly recommend codewars for you, but then we are going off topic. Lets focus in this topic on id123, and solving his problem


#11

I actually waited to see if id123 reply or not. As, the person did not write a comment so thought let's do this, otherwise the topic will be lost forever as you normally do not encourage commenting on a back dated topic. No more excuses, my bad.


#12

Thanks everyone for the comments.

  for(var i= 0; i < half; i++){
    if(str[i] !== str[len - 1- i]) {
      return false;
    }
}
      return true;
     
}
palindrome("eye"); // true
palindrome("race car"); // true
palindrome("not a palindrome"); // false
palindrome("A man, a plan, a canal. Panama")// true.

Not all arguments are passing the test. You see "race car" will return false as it should return true because it is a palindrome

Do we also have to consider spaces. If not how can we fix this issue.


#13

I think you have to consider spaces and punctuations. The "alphatest" function in my code actually getting rid of the spaces and punctuations. Also you have to consider uppercase/lowercase.


#14

race car returns false, where racecar return true, as you can see here, just press run in the top right corner, so your code works fine


#15

why is upper case and lowercase needed

you are right though about removing spaces and non alphanumeric chars

so space increases the str length

we can use Regexp to get rid of this

but is that a robust code practice in such problems

in what scenarios do we use palindrome in real world app building


#16

you do want race car to evaluate to true? Then i would use a regex expression to remove all no alphanumeric characters, which you can find here


#17

Uppercase/lowercase needed so that you can use both "Race car" and "race car". I am newbie in JavaScript character code.
But I think if someone use complicated symbols in the text :ox::four_leaf_clover::four_leaf_clover::ox: like this(symbols can be more complicated than this: non-english alphabets, different writing fonts) and now if you count the symbolic word as pallindrom then maybe "robust" coding practice will be more complicated.


#19
var str = prompt("Give us your strings. We will check if you are Palindrom Syndrom positive or not?").toUpperCase();
var z =[];
var y =[];

var alphatest = function(str)
{
    
    for (j=0; j<str.length; j++)
     {
        var code = str.charCodeAt(j);
         if ((code > 64 && code < 91) | (code > 96 && code < 123)) 
         
                 z.push(str[j]);
            
          else
               y.push(str[j]);
        
        
     }

};
alphatest(str);
var count = 0;
var len = z.length;
if(len<=2)
  console.log("Too short for Pallindrom!");
else{  
  var half = Math.floor(len / 2);

 
     
  for(var i=0;i< half;i++)
   
  {
    if((z[i] === z[(len-i)-1]) )
    {
        count +=1;
    }
    
    
}
}
if (count === half)
console.log("Positive");
else
console.log("Negative");
console.log("Non alpha characters:",y);