Instruction
string).
#11
Strings in Javascript include a method function named search() that will search for the first occurrence of any character in a search string. For example
index = phrase.search(/[ .,;:'!\?]/);
finds the location of the first occurrence of a space or punctuation mark in the string named phase. The value of index is set to location of the character in the string or is set to -1 otherwise.
We can use this function to write our own function for removing spaces and punctuation characters from a string. The algorithm for doing this task is as follows:
ALGORITHM: Remove spaces and punctuation marks from a string
PROCEDURE:
index = phrase.search(/[ .,;:'!\?]/);
WHILE a space or punctuation character is not found
phrase = phrase.substring(0,index) +
phrase.substring(index+1,phrase.length)
index = phrase.search(/[ .,;;'!\?])
END WHILE
Write a Javascript function strip() that takes a string as an input parameter and returns a string with all spaces and punctuation marks removed. Attach a PDF containing the source code to this function as your response to this question.