Shortest Word
๋ฌธ์ ์ดํด
Simple, given a string of words, return the length of the shortest word(s).
String will never be empty and you do not need to account for different data types.
๋ฌธ์ฅ์์ ๊ฐ์ฅ ์งง์ ๋จ์ด๋ฅผ ์ฐพ์๋ด์ด ๋จ์ด์ ์๋ฅผ return ํ๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ
๋จ์ด๋ฅผ Array ๋ฉ์๋์ธ split๋ฅผ ์ด์ฉํด ๋ฐฐ์ด์ ์์๋ก ๋ด๋๋ค.
for๋ฌธ์ ์ด์ฉํด ์์์ ๊ฐ์ฅ ์งง์ ๋จ์ด์ length๋ฅผ ๋ณ์์ ๋ด๋๋ค.
length๋ฅผ ๋ด์ ๋ณ์๋ฅผ return ํ๋ค.
์ฝ๋ ๊ตฌํ
function findShort(s){
var array = s.split(' ');
var textLength = array[0].length;
for(var i = 1; i < array.length; i++) {
if(textLength > array[i].length) {
textLength = array[i].length;
}
}
return textLength;
}
๊ฒฐ๊ณผ ๋ถ์
ํต๊ณผ
codewars user's solution
sgmaster, Armand, naiqum, 3421412, robinab, kalamisu (plus 42 more warriors)
function findShort(s){
return Math.min.apply(null, s.split(' ').map(w => w.length));
}
function findShort(s){
return Math.min(...s.split(" ").map (s => s.length));
}
๋๊ฐ์ solution ๋ค Math,min์ ์ฌ์ฉํ์๋ค.
์ฐจ์ด๋ apply๋ฅผ ์ด์ฉํด ๋ฐฐ์ด์ ์ด๊ฑฐ ํ๊ฑฐ๋ spread ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์๋ค.
Last updated
Was this helpful?