1

So I have a program that counts the words in a document that is loaded into an iFrame, all of the newline characters are replaced by a space, then the string is split by spaces, I then want to add them to a map as the key and set their value to 1 if they're not in the map or not a space, this is where I'm having the problem because it's still counting each space in the string , I feel like I'm being really stupid and missing something obvious...

var innerDoc = document.getElementById("pageinframe");
var innerDocContent = innerDoc.contentDocument.body.innerHTML;
var split = strip(innerDocContent).replace(/(\r\n|\n|\r)/gm, " ").split(" ");
var obj = new Map();
for (var x = 0; x < split.length; x++) {
     console.dir(typeof split[x]);
     if(!obj.has(split[x]) && (split[x] != " ")) {
         obj.set(split[x], 1);
      }
      else if (split[x] != " ") {
         obj.set(split[x], obj.get(split[x])+1);
      }
}

function strip(str) {
    var tmp = document.createElement("DIV");
    tmp.innerHTML = str;
    return tmp.textContent || tmp.innerText || "";
}
2
  • 1
    If you use 'split' doesn't it already give you an array of the words, without the space characters ? Commented Feb 15, 2017 at 8:55
  • 1
    Would it be easier to use /\b/g to find words ? Commented Feb 15, 2017 at 8:58

3 Answers 3

1

There are something, icant understand. When you split(' '). the array not contains ' ', only words!!!, https://www.w3schools.com/jsref/jsref_split.asp. Son Why need to testif is white space. Then i think, it could be in:

.replace(/(\r\n|\n|\r)/gm, " ")
Sign up to request clarification or add additional context in comments.

Comments

0

The split array contained "" not " " like I originally thought. Like I said I was missing something really obvious, just needed to see what was in the array.

Comments

0
var innerDoc = document.getElementById("pageinframe");

var innerDocContent = innerDoc.contentDocument.body.innerHTML;

var split = strip(innerDocContent).replace(/(\r\n|\n|\r)/gm, " ").split(" ");
var obj = new Map();
for (var x = 0; x < split.length; x++) {

     // Removing NaN for string
     if(split[x].charCodeAt(0))
     {
         if(!obj.has(split[x]) && (split[x] != " ")) {
             obj.set(split[x], 1);
          }
          else if (split[x] != " ") {
             obj.set(split[x], obj.get(split[x])+1);
          }
    }
}

function strip(str) {
    var tmp = document.createElement("DIV");
    tmp.innerHTML = str;
    return tmp.textContent || tmp.innerText || "";
}

console.log(obj);

1 Comment

I hope this will help you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.