Question

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.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top