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."));
4
  • And what is the problem ? Commented Jul 20, 2017 at 6:56
  • i think it should be just return spaces; Commented Jul 20, 2017 at 6:57
  • 1
    Possible duplicate of Javascript: How many times a character occurs in a string Commented Jul 20, 2017 at 7:05
  • Not a duplicate; needed help in a function format. Commented Jul 20, 2017 at 7:25

9 Answers 9

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 );
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

It was intentionally done but not needed. Thanks for the help.
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>

1 Comment

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.
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."));

Comments

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)

Comments

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."));

Comments

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)

Comments

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."));

Comments

-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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.