1

I am working on a book and it is asking to create a function to find the spaces in a string. Not sure what I am doing wrong, but here is the code I have.

function calSpaces(str) {
  var spaces = 0;

  for(var i = 0; i < str.length; i++) {
    if (str[i] === ' ') {
       spaces ++;
  }
  return spaces - 1;
}

console.log(calSpaces("This is a test of spaces."));

9 Answers9

1

You can do one trick like this:

var st = "Good morning people out there."
var result = st.split(' ');
var space_count = result.length-1;
console.log( space_count );
Himanshu Upadhyay
  • 6,438
  • 1
  • 16
  • 32
1

Your logic is working but you just miss one curly bracket in if condition.

function calSpaces(stringline)
{
  var spaces = 0;

  for(var i = 0; i < stringline.length; i++) 
  {
    if (stringline[i] === ' ') {
       spaces ++;
    }
  }
  return spaces - 1;
}

Just add ending curly bracket and problem solved.

Also, returning space have total count - 1. is this intentionally done? if not then please remove - 1 from the count.

Here is the JSBIN link

happy coding.

Sahil Patel
  • 1,491
  • 14
  • 31
1

A pretty simple solution is to use regex that will match spaces (and/or all whitespaces):

function countSpaces(str) {
  return (str.match(/\s/g) || []).length;
}

function showCount() {
  var str = document.getElementById('string').value;
  document.getElementById('count').innerHTML = countSpaces(str);
}
<input type="text" id="string">
<button onclick="showCount()">Count Spaces</button>
<span id="count"></span>
elegisandi
  • 389
  • 2
  • 10
  • While this code snippet may answer the question, it would probably benefit from a short explanation as to what you are doing, for example, explaining it uses regex to solve the problem, and maybe showing some output in the form of a stack snippet. - From Review. – Rob Quincey Jul 20 '17 at 07:53
0

Here is how you can do it:

 var my_string = "This is a test of spaces.";
 var spaceCount = (my_string.split(" ").length - 1);
 console.log(spaceCount)
Fahad Nisar
  • 1,673
  • 11
  • 18
0

Check your braces, one is missing

function calSpaces(str) {
  var spaces = 0;

  for(var i = 0; i < str.length; i++) {
    if (str[i] === ' ') {
       spaces ++;
  }//end of IF
  return spaces - 1;
}//end of FOR
//??? end of FUNCTION ???
console.log(calSpaces("This is a test of spaces."));

You were using return inside your for loop.

You only need to return spaces not spaces - 1

function calSpaces(str) {
  var spaces = 0;

  for (var i = 0; i < str.length; i++) {
    if (str[i] === ' ') {
      spaces++;
    }
  }
  return spaces;//Outside of loop
}


console.log(calSpaces("This is a test of spaces."));
Weedoze
  • 12,968
  • 1
  • 34
  • 57
0

A simpler solution is using regex to extract only the spaces from the string and count them:

function calSpaces(str) {
  return str.replace(/[^\s]/g, '').length;
}

console.log(calSpaces("This is a test of spaces."));
Alberto Trindade Tavares
  • 9,248
  • 5
  • 33
  • 45
0

Probably the simplest and shortest solution would be to use split() and get the length of an array:

var string = "This statement has lot of spaces and this spaces are never ending.";
var count = (string.split(' ').length - 1);
console.log(count)
Milan Chheda
  • 8,009
  • 3
  • 19
  • 35
0

There are other ways of doing that as other answers point out, but to answer your question on your exact problem: your parentheses are wrong. Try this snippet, now it works:

function calSpaces(str) {
      var spaces = 0;
    
      for(var i = 0; i < str.length; i++) {
        if (str[i] === ' ') {
           spaces++;
       }
      }

      return spaces - 1;
    }

    console.log(calSpaces("This is a test of spaces."));
Rob
  • 11,041
  • 13
  • 54
  • 89
-2

I would suggest an easier/faster approach using regex:

function calSpaces(str) {
    count = (str.match(/\s/g) || []).length;
    return count;
}

console.log(calSpaces('This is an example string'));
Philipp Sander
  • 9,891
  • 5
  • 43
  • 78