Refactored and validated version of String Functions using JS