I need help understanding why my sortproperties function isnt recieving the object

function loadBook(filename, displayName) {
  var currentBook = ""
  let url = "books/" + filename;

  //reset UI

  document.getElementById("fileName").innherhtml = displayName;
  document.getElementById("searchstat").innerHTML = "";
  document.getElementById("keyword").value = "";

  // create a server request to load our book

  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.send();
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      currentBook = xhr.responseText;
      getDocStats(currentBook);

      //remove line breaks and carriage returns and replace with <br>

      currentBook = currentBook.replace(/(?:\r\n|\r|\n)/g, '<br>');
      document.getElementById("fileContent").innerHTML = currentBook;

      var elmnt = document.getElementById("fileContent");
      elmnt.scrollTop = 0;

    }
  }
}

//this function takes in the text file and takes a count of every time a single word is used and then puts it into an object

function getDocStats(fil) {
  var doclength = document.getElementById("docLength");
  var wordcount = document.getElementById("wordCount");
  var charcount = document.getElementById("charCount");
  let text = fil.toLowerCase();

  let wordArray = text.match(/\b\S+\b/g);
  let wordDictionary = {};

  for (let word in wordArray) {
    let wordValue = wordArray[word];
    if (wordDictionary[wordValue] > 0) {
      wordDictionary[wordValue] += 1;
    } else {
      wordDictionary[wordValue] = 1;
    }

  }

  // these variables are used when sortProperties() works

  /*
     var top5wrds = wordlist.slice(0,6);
     var least5wrds = wordlist.slice(-6,wordlist.length)
     Ultemplate(top5wrds,document.getElementById("mostUsed"));
     Ultemplate(least5wrds,document.getElementById("leastUsed"));
  */

  let wordlist = sortProperties(wordDictionary);

}

//this function is supposed to take the object created in GetDocStats and convert into an array and then sort the array from greatest to least
// this function is not getting the object created in the getDocstats. this is my problem.

function sortProperties(obj) {
  let reddit = Object.defineProperties(obj);
  /*rtnArray.sort(function (first,second){
      return second[1] - first[1];
  });
  */
  return reddit;
}

/*
function Ultemplate(items,element){
    let rowTemplate = document.getElementById('template-ul-items');
    let templateHTML = rowTemplate.innerHTML;
    let resultsHTML = "";
    for(i=0;i<items.length-1;i++){
        resultsHTML += templateHTML.replace('{{val}}',items[i][0] + " : " + items[i][0]);
    }
    element.innerHTML = resultsHTML;
}

*/

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Just paring everything down to the bare essentials, it does appear to get it:

function getDocStats(fil) {
  let text = fil.toLowerCase();
  let wordArray = text.match(/\b\S+\b/g);
  let wordDictionary = {};
  for (let word in wordArray) {
    let wordValue = wordArray[word];
    if (wordDictionary[wordValue] > 0) {
      wordDictionary[wordValue] += 1;
    } else {
      wordDictionary[wordValue] = 1;
    }
  }
  sortProperties(wordDictionary);
}

function sortProperties(obj) {
  console.log(JSON.stringify(obj, null, 2));
  // {
  //   "how": 1,
  //   "much": 1,
  //   "wood": 2,
  //   "would": 1,
  //   "a": 2,
  //   "woodchuck": 2,
  //   "chuck": 2,
  //   "if": 1,
  //   "could": 1
  // }
}

getDocStats('How much wood would a woodchuck chuck if a woodchuck could chuck wood?')

What is sortProperties getting in your case? What is it getting right before it is passed? Keep tracing backwards until what you are getting finally matches with what you expect. Where what you expect diverges from what you’re getting - that’s where you bug is.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.