본문 바로가기
코테연습

9.Shortest Word

by hxunz 2022. 3. 18.

Description:

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.

 

function findShort(s){
  const words = s.split(' ');
 
  const short = words.reduce((previousValue, currentValue) => {
    return currentValue.length < previousValue.length ? currentValue : previousValue;
  });
  return short.length;
}

주어진 문장을 split()를 사용해서 공백을 기준으로 나눠서 새로운 리스트를 만들었다.

그다음에 reduce()를 사용해서 해봤다,,,ㅋ 

reduce 잘 몰라서 mdn 보고 콘솔 찍어보면서 현재 단어랑 다음 단어랑 길이를 비교하는 삼항 연산자를 사용해서 풀었다.

 

 

 

'코테연습' 카테고리의 다른 글

11.Complementary DNA  (0) 2022.03.20
10.Jaden Casing Strings  (0) 2022.03.20
8.Exes and Ohs  (0) 2022.03.18
7.List Filtering  (0) 2022.03.17
6.Isograms  (0) 2022.03.16

댓글