-1
function palindrome(str) {
  var strReverse=str.toLowerCase().replace(/\W+/g,'').split('').reverse().join('');
  if(strReverse===str)
    return true;
  else
    return false;
}

This program should check for the following palindromes:

palindrome("eye") should return true.

palindrome("race car") should return true.

palindrome("not a palindrome") should return false.

palindrome("A man, a plan, a canal. Panama") should return true.

palindrome("never odd or even") should return true.

palindrome("nope") should return false.

palindrome("almostomla") should return false.

palindrome("My age is 0, 0 si ega ym.") should return true.

palindrome("1 eye for of 1 eye.") should return false.

palindrome("0_0 (: /-\ :) 0-0") should return true.

But its not working for the multi string lines. I think its because of the RegEx but I cannot seem to find what is exactly wrong.

Munawir
  • 3,346
  • 9
  • 33
  • 51
Tannia
  • 89
  • 1
  • 2
  • 9

2 Answers2

2

You're using toLowerCase when creating strReverse, and then comparing it with str. But str will still have upper-case characters in it. Additionally, you're leaving the non-alpha characters in str.

You also need to remove _ for the last one to work, which \W won't do on its own.

You need to first prep str, then create the reversed version of it, and do the check:

function palindrome(str) {
    var strReverse;
    str = str.toLowerCase().replace(/\W|_+/g,'');
    strReverse = str.split('').reverse().join('');
    return strReverse === str;
}

Live Example:

function palindrome(str) {
    var strReverse;
    str = str.toLowerCase().replace(/\W|_+/g,'');
    strReverse = str.split('').reverse().join('');
    return strReverse === str;
}
function test(str, expectedResult) {
    var result = palindrome(str);
    var p = document.createElement('p');
    p.className = !result == !expectedResult ? "good" : "bad";
    p.appendChild(document.createTextNode(str));
    document.body.appendChild(p);
}
test("eye", true);
test("race car", true);
test("not a palindrome", false);
test("A man, a plan, a canal. Panama", true);
test("never odd or even", true);
test("nope", false);
test("almostomla", false);
test("My age is 0, 0 si ega ym.", true);
test("1 eye for of 1 eye.", false);
test("0_0 (: /-\ :) 0-0", true);
.good {
  color: green;
}
.bad {
  color: #d00;
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Try this function

function palindrome(str) {
    str=str.toLowerCase().replace(/[^A-Za-z]+/g, '');
    var strReverse = str.split('').reverse().join('');
    if(strReverse===str)
        return true;
    else
        return false;
}

Issue in your code was that when you were comparing the strReverse with str, strReverse was formatted but str was not. Like you didn't remove special characters and spaces and numbers from str, you didn't made it lowercase etc.

Check this fiddle for every test case

void
  • 36,090
  • 8
  • 62
  • 107