Skip to main content

Ivan Teoh

Something personal yet public

171-exercise62.js

(Source)

function processParagraph(paragraph) {
    // check whether this paragraph is header
    var typeString = 'p';
    var header = 0;
    var i;
    var content;
    for (i = 0; i < paragraph.length; i++) {
        if (paragraph.charAt(i) === '%') {
            header += 1;
        } else {
            break;
        }
    }

    if (header) {
        typeString = 'h' + header;
        content = paragraph.substr(header + 1);
    } else {
        content = paragraph.substr(header);
    }



    return {'content': content, 'type': typeString};
}
// {'content': 'Language', 'type': 'h2'}
//console.log(processParagraph("%% Language"));
// {'content': 'A hermit spent ten years writing a program.', 'type': 'p'}
//console.log(processParagraph("A hermit spent ten years writing a program."));
// {'content': 'The Book of Programming', 'type': 'h1'}
//console.log(processParagraph("% The Book of Programming"));