Last active
March 23, 2020 06:30
-
-
Save ms314006/6f14210868f2965bd1a511f5ff86ab25 to your computer and use it in GitHub Desktop.
This file contains 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 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