/**
 * Helper functions for custom text analytics
 */


// get each word's word count
function countEachWord(textInput) {
  // split up input string into arrays by spaces and newline chars
  const whiteSpaceChars = /\s/;
  const allWords = textInput.split(whiteSpaceChars);

  const wordCountObject = {};

  allWords.forEach((currentWord) => {
    // get rid of punctuation and make word lowercase
    const editedWord = currentWord.replace(/\W/g, '').toLowerCase();
    // account for empty strings (aka trailed whitespace)
    if (editedWord.length) {
      if (!wordCountObject[editedWord]) {
        wordCountObject[editedWord] = 1;
      } else {
        wordCountObject[editedWord] += 1;
      }
    }
  });

  return wordCountObject;
}


// find the top three most-used words, excluding 'the', 'a', 'an', and 'and'
function topThreeWords(wordCountObject) {
  const wordsToIgnore = /the\b|a\b|an\b|and\b/;
  const copiedObj = wordCountObject;
  const mostCommonWords = {};
  const wordKeys = Object.keys(copiedObj);
  let i = 0;

  wordKeys.forEach((word) => {
    if (word.match(wordsToIgnore)) {
      delete copiedObj[word];
    }
  });

  function findCurrentLargest(wordObj) {
    let largest = 0;
    let mostUsed = '';
    const currentKeys = Object.keys(wordObj);
    currentKeys.forEach((key) => {
      if (wordObj[key] > largest) {
        largest = wordObj[key];
        mostUsed = key;
      }
    });
    mostCommonWords[mostUsed] = largest;
    delete copiedObj[mostUsed];
  }


  while (i < 3) {
    if (Object.keys(copiedObj).length) {
      findCurrentLargest(copiedObj);
    }
    i++;
  }

  return mostCommonWords;
}

/* for the linter: */
const someWordObj = countEachWord('hi i am a owrkdwejlkjf oujfijlk  lkjh ihihihi hi hi hi');
const topThree = topThreeWords(someWordObj);
console.log(topThree);

@@ -267,17 +267,16 @@
 Chars);%0A
-%0A
   const 
@@ -736,17 +736,16 @@
 ect;%0A%7D%0A%0A
-%0A
 // find 
@@ -1490,17 +1490,16 @@
 %5D;%0A  %7D%0A%0A
-%0A
   while 
@@ -1640,166 +1640,590 @@
 %7D%0A%0A/
-* for the linter: */%0Aconst someWordObj = countEachWord('hi i am a owrkdwejlkjf oujfijlk  lkjh ihihihi hi hi hi');%0Aconst topThree = topThreeWords(someWordObj);
+/ checks to see if any 'avoid' words (words the user wants to avoid) were used%0Afunction checkWordsToAvoid(wordsToAvoidArr, allWordsUsedObj) %7B%0A  const wordsUsed = %7B%7D;%0A  wordsToAvoidArr.forEach((word) =%3E %7B%0A    if (allWordsUsedObj%5Bword%5D) %7B%0A      wordsUsed%5Bword%5D = allWordsUsedObj%5Bword%5D;%0A    %7D%0A  %7D);%0A  return wordsUsed;%0A%7D%0A%0A/* for the linter: */%0Aconst someWordObj = countEachWord('hi i am a owrkdwejlkjf oujfijlk  lkjh ihihihi hi hi hi');%0Aconst avoid = %5B'i'%5D;%0AcheckWordsToAvoid(avoid, someWordObj);%0Aconst topThree = topThreeWords(someWordObj); // apparently modifies original someWordObj
 %0Acon

Create function for checking if a 'avoid' words were used