Skip to content

Instantly share code, notes, and snippets.

@master-elodin
Created March 9, 2019 17:34
Show Gist options
  • Save master-elodin/ef5899ca2371d4def7a34a96e32e86a8 to your computer and use it in GitHub Desktop.
Save master-elodin/ef5899ca2371d4def7a34a96e32e86a8 to your computer and use it in GitHub Desktop.
/*
* #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