查找字符串中的单词数 JavaScript
javascriptweb developmentfront end technologyobject oriented programming更新于 2024/1/3 0:12:17
我们需要编写一个 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
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

