Pages

Monday, August 29, 2016

JS exercises - Palindrome

Write a recursive JS function to determine if a string is palindrome or not.

Return true if it is a palindrome, and false if it is not a palindrome.

I used regex to first get rid of the spaces and any puntuation.

For example :

Madam, I'm Adam - should return true, because it's the same backwards as forwards.

Answer in the comments.

1 comment:

  1. var test = "Madam I'm Adam";


    var paliChecker = function(str){
    var paliCheck = [];
    str = str.replace(/ |' |.|'?'|!/g, "").toLowerCase();
    var length = str.length;
    var num = 0;
    function poop(){
    if(paliCheck.length === length){
    return paliCheck.join("") === str;
    }
    num ++;
    paliCheck.push(str[str.length - num]);
    return poop();
    }
    return poop();

    }

    var maple = paliChecker(test);

    ReplyDelete