#Help me to understand this part of ccode, i dont understand how it works , since while to if

7 messages · Page 1 of 1 (latest)

wary badge
#

var longestCommonPrefix = function(strs) {
if (strs.length === 0) return "";
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.slice(0, -1);
if (!prefix) return "";
}
}
return prefix;
};

#

while (strs[i].indexOf(prefix) !== 0) {
prefix = prefix.slice(0, -1);
if (!prefix) return "";
this part

#

i dont understnad hpw indexOf works

lone relic
#

it finds where prefix exists inside strs[i], or -1 if it isn't present

#

though !strs[i].startsWith(prefix) would make more sense

#

see docs ^