jeudi 4 janvier 2018

Counting chars without comments

I'm trying to make a function that can count all chars in a string input, but without counting any comments (// or /*)

So far it works perfectly with lines after //. I splitted my String by every new line and I only count the lines chars if it doesn't include //

This is the code so far:

function findComments() {

    var string = document.getElementById("input").value;
    var splittedString = string.split("\n");

    var count = 0;

    for (var i = 0; i < splittedString.length; i++) {
        while (countStars) {
            if(splittedString[i].indexOf("*/") > -1) {
                countStars = false;
            }
            continue;
        }

        if(splittedString[i].indexOf("/*") > -1) {
            var countStars = true;
        }

        if(splittedString[i].indexOf("//") === -1) {

            var chars = splittedString[i].split("");
            for (var j = 0; j < chars.length; j++) {
                count++;
            }
        }

    }
    console.log(count);
}

As I mentioned, it should just continue the loop until it finds an ending of the comment ( */ ). So far this doesn't work

Any help is very much appreciated! :)

Aucun commentaire:

Enregistrer un commentaire