Code kata_day5

🙋🏻‍♀️·2022년 5월 18일
0

wecode

목록 보기
36/40

Code kata-Week1-Day5

const getPrefix = strs => {
    let prefix = strs[0];     //비교기준점 설정
    if(strs.length === 0){	 //빈배열을 위한 대비
        prefix = "";
    }
    for(let i=1; i<strs.length; i++){                        //for문
        while(strs[i].indexOf(prefix) !== 0){                // 이중for문하려고 했으나 여러번 반복해야 함으로 while
            prefix = prefix.substring(0, prefix.length-1)    // 프리픽스가 없거나, 단어 중간에 존재하면 단어를 뒤에서부터 하나씩 자른다;
        }
    }
return prefix;
}

module.exports = { getPrefix };

0개의 댓글