在字符串JavaScript中查找空格数
我们需要编写一个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
热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短