找到一个字符串中的单词数
我们需要编写一个 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP