Created
March 9, 2019 17:34
-
-
Save master-elodin/ef5899ca2371d4def7a34a96e32e86a8 to your computer and use it in GitHub Desktop.
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
/* | |
* #1 | |
*/ | |
let findLongestPrefix = (list) => { | |
let prefix = ''; | |
let charIndex = 0; | |
let allMatch = true; | |
while(allMatch) { | |
let currentChar = list[0][charIndex]; | |
for(let i = 1; i < list.length; i++) { | |
if(list[i][charIndex] !== currentChar) { | |
allMatch = false; | |
} | |
} | |
charIndex++; | |
if(allMatch) { | |
prefix += currentChar; | |
} | |
} | |
return prefix; | |
}; | |
console.log(`findLongestPrefix(['abcdefabc', 'abcdfff', 'abcodo'])=='abc' = ${findLongestPrefix(['abcdefabc', 'abcdfff', 'abcodo'])=='abc'}`); | |
console.log(`findLongestPrefix(['aab', 'adc', 'aef'])=='abc' = ${findLongestPrefix(['aab', 'adc', 'aef'])=='a'}`); | |
console.log(`findLongestPrefix(['abc', 'def', 'cdb'])=='abc' = ${findLongestPrefix(['abc', 'def', 'cdb'])==''}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment