0

I've been working on a function that changes the spaces between words into the string " "(space).

For example, "Hello World. Hi there." would become "Hello(space)world.(space)Hi(space)there."

EDIT: I'm trying to build this to a specific set of structured English which is as follows:

  • set the initial value of result to an empty string
  • for each index in the argument string
  • if the character at that index is a space then
  • append '(space)' to result
  • else
  • append the character at that index to result
  • end if
  • end for
  • return result

Here is what I was able to come up with so far.:

function showSpaces(aString)
{
var word, letter;

word = aString
for var (count = 0; count < word.length; count = count + 1)

{
    letter = word.charAt(count);
    if (letter == " ")
    {
        return("(space)");
    }
    else
    {
        return(letter);
    }
}
}

Whenever I test this function call, nothing happens:

<INPUT TYPE = "button" NAME = "showSpacesButton"  VALUE ="Show spaces in a string as (space)"
        ONCLICK = "window.alert(showSpaces('Space: the final frontier'));">

I'm just beginning with JavaScript at the moment. Any help would be appreciated.

-Ross.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
Ross Cheeseright
  • 117
  • 1
  • 1
  • 8
  • There's no need to include the focal topic in the title. StackOverflow will prefix the page's title with the most popular tag (in this case, JavaScript). –  Sep 13 '11 at 19:37
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 –  Sep 13 '11 at 19:38

2 Answers2

3

Use String.replace

function showSpaces(aString)
{
    return aString.replace(/ /g,'(space)');
}

EDIT: to get your code working:

function showSpaces (aString)
{

    var word, letter,
        output = ""; // Add an output string

    word = aString;
    for (var count = 0; count < word.length; count = count + 1) // removed var after for

    {
        letter = word.charAt(count);
        if (letter == " ")
        {
            output += ("(space)"); // don't return, but build the string
        }
        else
        {
            output += (letter); // don't return, but build the string
        }
    }
    return output; // once the string has been build, return it

}
Joe
  • 80,724
  • 18
  • 127
  • 145
  • Thanks for this great solution. My only problem is that I am trying to build it according to a specific set of structured English. set the initial value of result to an empty string for each index in the argument string if the character at that index is a space then append '(space)' to result else append the character at that index to result end if end for return result – Ross Cheeseright Sep 13 '11 at 19:37
  • @Ross, I've updated with comments in your algorithm. Let me know if this helps. – Joe Sep 13 '11 at 19:43
  • Thanks a lot for that. Those comments are really helpful! – Ross Cheeseright Sep 13 '11 at 19:45
1

No, "nothing" doesn't happen. It very rarely does. What happens is that you are getting a syntax error in the code because you used for var ( instead of for (var.

If you fix that, you will notice that you only get the first character in the string, as you use return inside the loop instead of putting together a string and returning it after the loop.

You can do like this:

function showSpaces(word) {
  var letter, result = "";
  for (var count = 0; count < word.length; count++) {
    letter = word.charAt(count);
    if (letter == " ") {
      result += "(space)";
    } else {
      result += letter;
    }
  }
  return result;
}

Demo: http://jsfiddle.net/Guffa/pFkhs/

(Note: Using += to concatenate strings performs badly for long strings.)

You can also use a regular expression to replace a string:

function showSpaces(word) {
  return word.replace(/ /g, "(space)");
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • a sound point you make about "Nothing not happening." I should look at things more closely. Many thanks for your tutorship explaining my "return." inside the loop, too. It's invaluable. – Ross Cheeseright Sep 13 '11 at 19:45