0

I have this function

function palindrome(str){
    var myStr = "";
    for (var index = str.length -1 ; index >=0; index--) {
        myStr += str[index]

    }
    var res = myStr == str ? true : false;

    return res;
}

It works with a single word , but if i checked such as "I did, did I?" , it don't works. How i can check is without using replace , join , reverse , etc... Only pure js . Thanks very much .

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    "How i can check is without using replace , join , reverse , etc... Only pure js". They are pure JavaScript functions! They can be used without loading any other libraries or frameworks. – phuzi Mar 23 '20 at 14:43
  • Does this answer your question? [Palindrome check in Javascript](https://stackoverflow.com/questions/14813369/palindrome-check-in-javascript) – Fraction Mar 23 '20 at 14:43
  • If you're including pontuation, you should remove them and get rid of the white spaces. And then compare with the reversal form – Greggz Mar 23 '20 at 14:45

1 Answers1

0

This is how i do it, run the loop both incremental and decremental fashion at the same time

function palindrome() {
  var str = "dd dd";
  var Ispalindrome = true;
  for (var i = 0, j = str.length - 1; i < str.length && j >= 0; i++, j--) {
    if (str[i] !== str[j]) {
      Ispalindrome = false;
    }
  }
  alert(Ispalindrome);
  return Ispalindrome;
}

//Invoked
palindrome()
Abdullah Abid
  • 1,541
  • 3
  • 10
  • 24