I'm new to Javascript and wrote the code below to determine if a string is a palindrome. I'm curious as to what is the most efficient way to accomplish the same task.
var isPalindrome = function (string) {
var leftString = [];
var rightString = [];
// Remove spaces in the string and convert to an array
var strArray = string.split(" ").join("").split("");
var strLength = strArray.length;
// Determine if the string is even or odd in length, then assign left and right strings accordingly
if (strLength % 2 !== 0) {
leftString = strArray.slice(0, (Math.round(strLength / 2) - 1));
rightString = strArray.slice(Math.round(strLength / 2), strLength);
} else {
leftString = strArray.slice(0, (strLength / 2));
rightString = strArray.slice((strLength / 2, strLength))
}
if (leftString.join("") === rightString.reverse().join("")) {
alert(string + " is a palindrome.");
} else {
alert(string + " is not a palindrome.")
}
}
isPalindrome("nurses run");