Created
August 4, 2018 14:08
-
-
Save umair-khanzada/141a16ae57494e9c9c2d65df1ca8219c to your computer and use it in GitHub Desktop.
A simple efficient and faster way of checking palindrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A jsben performance image
