Skip to content

Instantly share code, notes, and snippets.

@ms314006
Last active March 23, 2020 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms314006/6f14210868f2965bd1a511f5ff86ab25 to your computer and use it in GitHub Desktop.
Save ms314006/6f14210868f2965bd1a511f5ff86ab25 to your computer and use it in GitHub Desktop.
var longestCommonPrefix = function(strs) {
const firstStr = strs[0]; // (1)
let condition = true;
let count = -1;
while(condition) { // (2)
count += 1;
condition = strs.every(str => (
firstStr[count] === str[count]
));
if (count === firstStr.length) { // (3)
condition = false;
}
}
return firstStr.slice(0, count); // (4)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment