2

 function palindrome(str) { // "Hello there"
   str.toLowerCase(); // "hello there"
   str = str.replace(/\s/g, ""); // "hellothere"
   var a = str;
   a.split("").reverse().join(""); // a = "erehtolleh"
   return (str === a); // "hellothere" === "erehtolleh"
 }

 alert(palindrome("123432"));

I passed non -palindromic value 123432 but it returns true value. Anyone know what's wrong with my palindrome function? Would really appreciate if someone can check my logic on it.

2 Answers 2

4

You need to assign value of a as its an return function

function palindrome(str) {                       // "Hello there"
    str.toLowerCase();                               // "hello there"
    str = str.replace(/\s/g, "");                    // "hellothere"
    var a = str;                             
    a = a.split("").reverse().join("");                  // a = "erehtolleh"
    return (str === a);                             // "hellothere" == "erehtolleh"
}


alert(palindrome("malayalam"));

Sign up to request clarification or add additional context in comments.

2 Comments

Why is this altering "malayalam"? Do you mean to invoke the palindrome function?
oh Yes forgot to call the function ;0)
1

Try this , String is immuatble so you need to assign the reversed String to the variable or do compare with the original and reversed String like this.

function palindrome(str) { // "Hello there"
  str.toLowerCase(); // "hello there"
  str = str.replace(/\s/g, ""); // "hellothere"
  // var a = str.split("").reverse().join(""); // a = "erehtolleh"
  return (str === str.split("").reverse().join("")); // "hellothere" == "erehtolleh"
}


alert(palindrome("12321"));
alert(palindrome("1232"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.