Jason Loomis
Jason Loomis

Reputation: 37

Calculate spaces in string using Javascript function

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

Upvotes: 1

Views: 998

Answers (9)

elegisandi
elegisandi

Reputation: 474

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>

Upvotes: 1

Sahil Patel
Sahil Patel

Reputation: 1620

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.

Upvotes: 1

Weedoze
Weedoze

Reputation: 13953

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

Upvotes: 0

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

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

Upvotes: 1

Rob
Rob

Reputation: 11798

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

Upvotes: 0

Milan Chheda
Milan Chheda

Reputation: 8249

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)

Upvotes: 0

Alberto Trindade Tavares
Alberto Trindade Tavares

Reputation: 10396

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

Upvotes: 0

Philipp Sander
Philipp Sander

Reputation: 10249

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

Upvotes: -2

Fahad Nisar
Fahad Nisar

Reputation: 1733

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)

Upvotes: 0

Related Questions