Created
October 7, 2012 20:52
-
-
Save davidbauer/3849557 to your computer and use it in GitHub Desktop.
Word counter
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
function countIt() { | |
var wordcount = 0; | |
var text = document.wordcounter.allthosewords.value; | |
for (i = 0; i < text.length; i++) { | |
if (text[i] === " ") { | |
wordcount++; | |
} | |
// special code needed for the smartasses that type in only one word | |
// special code needed for the smartasses that end their text with one or more spaces | |
} | |
if (wordcount === 0) { | |
results.innerHTML = "Hey, champion. Can't you count to zero by yourself? How about pasting some real text." | |
} else if (0 < wordcount && wordcount < 500) { | |
results.innerHTML = "Your text consists of <i>" + text.length + " characters</i> and <i>" + (wordcount + 1) + " words</i>. Sorry, not impressed. <br/><br/> <img src='http://media.tumblr.com/tumblr_m8tmyoMNZA1ruser4.jpg'>"; | |
} else { | |
results.innerHTML = "Your text consists of <i>" + text.length + " characters</i> and <i>" + (wordcount + 1) + " words</i>. Which is pretty fucking impressive."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Vielen Dank für die ausführliche Kritik. Leuchtet alles ein. Und bestätigt mich insofern, dass ich im Rahmen meiner Möglichkeiten alles richtig gemacht habe.
text.match(/\w+\W*/g)
muss man ja erstmal kennen, so als n00b.V2: https://gist.github.com/3853898