我们需要编写一个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