Created
December 15, 2014 18:27
-
-
Save jsatk/f721c613f363794c754b to your computer and use it in GitHub Desktop.
Finds the longest prefix in an array of strings
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
| var longestPrefix = function (arr) { | |
| var firstString = arr[0], | |
| prefix = ''; | |
| for (var i = 0; i < firstString.length; i++) { | |
| for (var j = 1; j < arr.length; j++) { | |
| if (arr[j].charAt(i) !== firstString.charAt(i)) { | |
| return prefix; | |
| } | |
| } | |
| prefix += firstString.charAt(i); | |
| } | |
| return prefix; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment