Wednesday, July 5, 2017

Check for Palindrome in JavaScript

Here is the code:

(function(){
'use strict';
var testStringOne = "Madam";
var testStringTwo = "testing";
var testNull = null;
var testUndefined;
console.log(checkIfPalindrome(testStringOne));
console.log(checkIfPalindrome(testStringTwo));
console.log(checkIfPalindrome(testNull));
console.log(checkIfPalindrome(testUndefined));
function checkIfPalindrome(testStringOne){
if(!testStringOne){
return false;
}
var testArrayOne = testStringOne.toLowerCase().split("");
testArrayOne.reverse();
if(testArrayOne.toString() == testStringOne.toLowerCase().split("").toString()){
return true;
}else{
return false;
}
}

})();

No comments:

Post a Comment