Created
October 22, 2016 20:27
-
-
Save d7my11/134d4e86277a6e2af85ca7d5938bfb8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var doc = "practice makes perfect. get perfect by practice. just practice!;"; | |
var wordCountEngine = { | |
init: function (doc) { | |
var newDoc = this.removePunctuation(doc); | |
newDoc = newDoc.split(" "); | |
var cleanDoc = newDoc; | |
newDoc = this.getSolidDoc(newDoc); | |
var docWithCount = newDoc.map(function(word){ | |
return wordCountEngine.getWordObject(word, wordCountEngine.getWordCount(cleanDoc, word)); | |
}); | |
console.log(docWithCount); | |
console.log(this.getOrderedList(docWithCount)); | |
}, | |
removePunctuation: function (doc) { | |
return doc.replace(/[.,!#;{}]/g, ""); | |
}, | |
getSolidDoc: function (list) { | |
var solidList = []; | |
list.forEach(function(word){ | |
if (solidList.indexOf(word) == -1) { | |
solidList.push(word) | |
} | |
}); | |
return solidList; | |
}, | |
getWordCount: function (doc, word) { | |
var count = 0; | |
for (var i = 0; i < doc.length; i++) { | |
if (doc[i] == word) { | |
count++; | |
} | |
} | |
return count; | |
}, | |
getWordObject: function (word, count) { | |
var obj = {}; | |
Object.defineProperty(obj, word, {value:count}); | |
return obj; | |
}, | |
getOrderedList: function (list) { | |
console.log(list) | |
var ordered = {}; | |
for (var i in list) { | |
if (!ordered.length) { | |
ordered = Object.assign(ordered, list[i]); | |
} else { | |
for (var j in ordered) { | |
if (list[i] > ordered[j]) { | |
console.log(list[i] , ordered[j]) | |
} | |
} | |
} | |
} | |
} | |
}; | |
wordCountEngine.init(doc); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment