mercredi 3 janvier 2018

Find commented out code

i'm working on a code that allows the user to check his/her code for a total number of chars, without counting any comment ( // and /* )

So far, i'ts counting correct on every // and also on /* except, it will start counting in the next line after /*, and it should wait until it receives a */ (end of comments).

function findComments() {

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

  var count = 0;

  for (var i = 0; i < splittedString.length; i++) {

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

      var chars = splittedString[i].split("");
      for (var j = 0; j < chars.length; j++) {
        count++;
      }
    }
  }
  console.log(count);
}
<button onclick="findComments()">Find Comments</button>
<textarea rows="10" cols="40" id="input"></textarea>

Aucun commentaire:

Enregistrer un commentaire