문제
우리는 단어와 숫자의 문장을 취하는 JavaScript 함수를 작성해야 합니다. 함수는 숫자로 지정된 길이보다 큰 모든 단어의 배열을 반환해야 합니다.
입력
const str = 'this is an example of a basic sentence'; const num = 4;
출력
const output = [ 'example', 'basic', 'sentence' ];
길이가 4보다 큰 유일한 세 단어이기 때문입니다.
예시
다음은 코드입니다 -
const str = 'this is an example of a basic sentence';
const num = 4;
const findLengthy = (str = '', num = 1) => {
const strArr = str.split(' ');
const res = [];
for(let i = 0; i < strArr.length; i++){
const el = strArr[i];
if(el.length > num){
res.push(el);
};
};
return res;
};
console.log(findLengthy(str, num)); 출력
[ 'example', 'basic', 'sentence' ]