Skip to content

Instantly share code, notes, and snippets.

@umair-khanzada
Created August 4, 2018 14:08
Show Gist options
  • Select an option

  • Save umair-khanzada/141a16ae57494e9c9c2d65df1ca8219c to your computer and use it in GitHub Desktop.

Select an option

Save umair-khanzada/141a16ae57494e9c9c2d65df1ca8219c to your computer and use it in GitHub Desktop.
A simple efficient and faster way of checking palindrome
function palindrome(str) {
str = str.toLowerCase(); //convert to lowercase
let count = Math.ceil(str.length / 2), //limit for itration
flag = true; //palindrome or not
for(var i = 0; i < count; i++){
//checking 0 and the last index are same or not
//if not same break
if(!(str[i] === str[str.length - i - 1])){
flag = false;
break;
}
}
return flag;
}
palindrome("something"); //false
palindrome("madam") //true
@umair-khanzada
Copy link
Author

A jsben performance image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment