Created
February 4, 2015 21:29
-
-
Save lmccart/b7b10dc6dad6736aefc9 to your computer and use it in GitHub Desktop.
2/4 class snippets
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
var text = "The shore line was peaceful and flat, and the calm sea bumped it playfully along the sandy beach. In the distance a beautiful island covered with palm trees and flowers beckoned invitingly from the sparkling water. Nothing can possibly go wrong now, cried the Humbug happily, and as soon as he'd said it he leaped from the car, as if stuck by a pin, and sailed all the way to the little island. And we'll have plenty of time,'' answered Tock, who hadn't noticed that the bug was missing--and he, too, suddenly leaped into the air and disappeared. It certainly couldn't be a nicer day, agreed Milo, who was too busy looking at the road to see that the others had gone. And in a split second he was gone also."; | |
var words = text.split(" "); | |
var sentences = text.split(/[.?!;:]/); | |
//var sentences = text.split(/and|if|the/); | |
console.log("There are "+words.length+" words."); | |
console.log("There are "+sentences.length+" sentences."); | |
var long_words = []; | |
for (var i=0; i<words.length; i++) { | |
if (words[i].length > 5) { | |
long_words.push(words[i]); | |
} | |
} | |
console.log("There are "+long_words.length+" words with 6 letters or more."); | |
var count = 0; | |
var terms = ["and", "the", "it"]; | |
for (var i=0; i<words.length; i++) { | |
var w = words[i].toLowerCase(); | |
for (var j=0; j<terms.length; j++) { | |
if (w == terms[j]) { | |
count++; | |
} | |
} | |
} | |
for (var i=0; i<words.length; i++) { | |
var w = words[i].toLowerCase(); | |
if (terms.indexOf(w) !== -1) { | |
count++; | |
} | |
} | |
console.log("The word and/the/it is used "+count+" times."); | |
var keys = []; | |
var concordance = {}; | |
for (var i=0; i<words.length; i++) { | |
var w = words[i].toLowerCase(); | |
if (concordance[w] == undefined) { | |
concordance[w] = 1; | |
keys.push(w); | |
} else { | |
concordance[w]++; | |
} | |
} | |
keys.sort(function(a, b) { | |
if (concordance[b] > concordance[a]) { | |
return 1; | |
} else { | |
return -1; | |
} | |
}); | |
for (var i=0; i<keys.length; i++) { | |
console.log(keys[i]+":"+concordance[keys[i]]); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment