Shortest Word

Shortest Word Link

๋ฌธ์ œ ์ดํ•ด

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?