class JavaScriptCommentRemover {

    public String removeComments(String sourceCode) {
        state = State.IN_CODE;
        lastCodeStart = 0;
        codeSections = new ArrayList<>();

        for (int index = 0; index < sourceCode.length(); index++) {
            char character = sourceCode.charAt(index);
            handleCharacter(character, index);
        }

        if (lastCodeStart != -1) {
            codeSections.add(new Section(lastCodeStart, sourceCode.length()));
        }

        StringBuilder builder = new StringBuilder();
        for (Section section : codeSections) {
            builder.append(sourceCode.subSequence(section.getBegin(), section.getEnd()));
        }
        return builder.toString();
    }

}