Skip to content

Instantly share code, notes, and snippets.

@wyattdanger
Created May 22, 2012 15:40
Show Gist options
  • Save wyattdanger/2769826 to your computer and use it in GitHub Desktop.
Save wyattdanger/2769826 to your computer and use it in GitHub Desktop.
function longestCommonPrefix ( arr ) {
var result = "";
var firstWord = arr[0];
for ( var i = 0; i < firstWord.length; i++ ) {
for ( var j = 0; j < arr.length; j++) {
if ( firstWord[i] === arr[j][i]) {
continue;
} else {
return result;
}
}
result += firstWord[i];
}
}
// longestCommonPrefix(['hello', 'hellos', 'helium', 'help']) => 'hel'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment