Skip to content

Instantly share code, notes, and snippets.

@sergeyromanov
Last active August 29, 2015 14:10
Show Gist options
  • Save sergeyromanov/6c12b72556536662dcd5 to your computer and use it in GitHub Desktop.
Save sergeyromanov/6c12b72556536662dcd5 to your computer and use it in GitHub Desktop.
// jsbootcamp, task0
// by Sergey Romanov
var text = " \n\
The sky above the port was the color of television, tuned \n\
to a dead channel. \n\
\n\
`It's not like I'm using,' Case heard someone say, as he \n\
shouldered his way through the crowd around the door of the \n\
Chat. `It's like my body's developed this massive drug defi- \n\
ciency.' It was a Sprawl voice and a Sprawl joke. The Chatsubo \n\
was a bar for professional expatriates; you could drink there \n\
for a week and never hear two words in Japanese. \n\
\n\
Ratz was tending bar, his prosthetic arm jerking monoto- \n\
nously as he filled a tray of glasses with draft Kirin. He saw \n\
Case and smiled, his teeth a webwork of East European steel \n\
and brown decay. Case found a place at the bar, between the \n\
unlikely tan on one of Lonny Zone's whores and the crisp naval \n\
uniform of a tall African whose cheekbones were ridged with \n\
precise rows of tribal scars. `Wage was in here early, with two \n\
joeboys,' Ratz said, shoving a draft across the bar with his \n\
good hand. `Maybe some business with you, Case?' \n\
";
// split is not smart enough to remove white substrings at the beginning
// and at the end of string, hence trim; filter out apstrophes, although
// treat conjunctions like "It's", "I'm" etc. as single words
var words = text.trim()
.split(/[\s.,():;!?`]+/)
.filter(function(el) { return el && el !== "'" });
// doesn't work without initialization?! dynamic typing my ass... :(
var stat = {};
words.forEach(
function(el) {
stat.hasOwnProperty(el.length)
? stat[el.length]++
: stat[el.length] = 1;
});
// so, ECMA-262 does not guarantee the order of properties traversal?
// why VMs bother sorting them, then? (/me wonders)
for (var len in stat) {
console.log(
len, "letters:", stat[len], "word(s)",
"(" + stat[len]*100/words.length +"%)"
); // formatting numbers is TODO, right? ;)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment