Skip to content

Instantly share code, notes, and snippets.

@davidbauer
Created October 7, 2012 20:52
Show Gist options
  • Save davidbauer/3849557 to your computer and use it in GitHub Desktop.
Save davidbauer/3849557 to your computer and use it in GitHub Desktop.
Word counter
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.";
}
}
@davidbauer
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment