查找字符串中的空格数 JavaScript
javascriptweb developmentfront end technologyobject oriented programming更新于 2024/1/11 23:08:17
我们需要编写一个 JavaScript 函数,该函数接受包含空格的字符串。该函数应该只计算该字符串中存在的空格数。
例如 −
如果输入字符串是−
const str = 'this is a string';
那么输出应该是 −
const output = 4;
示例
const str = 'this is a string';
const countSpaces = (str = '') => {
let count = 0;
for(let i = 0;
i < str.length; i++){
const el = str[i];
if(el !== ' '){
continue; };
count++; };
return count;
};
console.log(countSpaces(str));
输出
这将产生以下输出 −
4
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

