It counts the repetition of words for a text inserted in the textarea. Simple but good to have
Last active
February 7, 2016 20:19
-
-
Save konstantinos-tsatsarounos/22063065f69476983f88 to your computer and use it in GitHub Desktop.
Text word counter
This file contains hidden or 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
| html { | |
| font-size: 16px; | |
| font-family: sans-serif; | |
| } | |
| button { | |
| border: none; | |
| background: royalblue; | |
| padding: 1em 2em; | |
| margin: .5em 0; | |
| color: white; | |
| font-size: 130%; | |
| } | |
| textarea { | |
| display: block; | |
| width: 500px; | |
| height: 200px; | |
| background-color: lightblue; | |
| } | |
| ul { | |
| list-style: none; | |
| padding: 2em 0 2em 2em; | |
| } | |
| li { | |
| display: inline-block; | |
| background-color: aqua; | |
| color: #555; | |
| padding: .5em; | |
| margin: .1em .2em; | |
| line-height: 1.2em; | |
| } | |
| #results { | |
| width: 500px; | |
| } |
This file contains hidden or 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
| function _$(select){ | |
| return document.querySelector(select); | |
| } | |
| function analyzeText(text){ | |
| var words = removeSpecial( text.split(' ') ); | |
| var wordRecord = []; | |
| var result = {}; | |
| words.map(function (current, index) { | |
| if(wordRecord.indexOf(current) == -1){ | |
| console.log("ok"); | |
| wordRecord.push(current); | |
| result[current] = 1; | |
| } else { | |
| result[current]++; | |
| } | |
| }); | |
| return result; | |
| } | |
| function removeSpecial(words){ | |
| var specials = ['.', '/', '!', ',', '\'', ';']; | |
| return words.map(function(current, index){ | |
| var count = 0; | |
| var output = ''; | |
| for(;count < specials.length; count = count + 1){ | |
| var special; | |
| if(current.indexOf(special = specials[count]) > -1) { | |
| current = current.replace(special, ''); | |
| } | |
| } | |
| return current.trim().toLocaleLowerCase(); | |
| }); | |
| } | |
| function generateList(TextDataObject, container){ | |
| var list = document.createElement('ul'), | |
| item; | |
| for(item in TextDataObject){ | |
| var li = document.createElement('li'); | |
| li.innerText = item + ": " + TextDataObject[item]; | |
| list.appendChild(li); | |
| } | |
| container.innerHTML = ''; | |
| container.appendChild(list); | |
| } | |
| _$('#analyze').addEventListener('click', function(){ | |
| var text = _$('#text').value; | |
| var results = _$('#results'); | |
| var analyzedText = analyzeText(text); | |
| generateList(analyzedText, results); | |
| }); |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Text Analyzer</title> | |
| <link href="style.css" rel="stylesheet" media="all" /> | |
| </head> | |
| <body> | |
| <textarea id="text"></textarea> | |
| <button id="analyze">Analyze</button> | |
| <div id="results"> | |
| </div> | |
| <script src="app.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment