임의의 길이의 문자열을 받는 JavaScript 함수를 작성해야 합니다. 그런 다음 함수는 해당 문자열의 단어 수를 계산해야 합니다.
예시
const str = 'THis is an example string';
const findWords = (str = '') => {
if(!str.length){
return 0;
};
let count = 1;
for(let i = 0; i < str.length; i++){
if(str[i] === ' '){
count++;
};
};
return count;
};
console.log(findWords(str)); 출력
콘솔의 출력은 -
5