Created
December 23, 2015 17:08
-
-
Save w3cj/bd79963f4af75ffd73e0 to your computer and use it in GitHub Desktop.
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
| // Output: | |
| // | |
| // { | |
| // "optimize" : { count: 1, people: ["Ila Huels"] }, | |
| // "web-enabled" : { count: 1, people: ["Ila Huels"] }, | |
| // "supply-chains" : { count: 1, people: ["Ila Huels"] }, | |
| // "brand" : { count: 2, people: ["Ila Huels", "Cristopher Feest"] }, | |
| // "sexy" : { count: 2, people: ["Ila Huels", "Cristopher Feest"] }, | |
| // "channels" : { count : 2, people : ["Ila Huels", "Cristopher Feest"] }, | |
| // "envisioneer" : { count: 1, people: ["Ila Huels"] }, | |
| // "robust" : { count: 1, people: ["Ila Huels"] }, | |
| // "e-commerce" : { count: 1, people: ["Ila Huels"] }, | |
| // "transform" : { count: 1, people: ["Ila Huels"] }, | |
| // "wireless" : { count: 1, people: ["Ila Huels"] }, | |
| // "architectures" : { count: 1, people: ["Ila Huels"] }, | |
| // "benchmark" : { count: 3, people: ["Ila Huels", "Cristopher Feest"] }, | |
| // "cross-platform" : { count: 2, people: ["Ila Huels", "Cristopher Feest"] }, | |
| // "partnerships" : { count: 2, people: ["Ila Huels", "Cristopher Feest"] }, | |
| // "24/7" : { count: 1, people: ["Cristopher Feest"] }, | |
| // "paradigms" : { count: 1, people: ["Cristopher Feest"] } | |
| // } | |
| function wordCount(input) { | |
| // create object to store results | |
| var result = {}; | |
| //Iterate over object to get peoples names | |
| for(var person in input) { | |
| //Iterate over each quote | |
| for (var i = 0; i < input[person].length; i++) { | |
| //Split each quote into words | |
| var quote = input[person][i].toLowerCase(); | |
| var words = quote.split(' '); | |
| // iterate over words | |
| for (var j = 0; j < words.length; j++) { | |
| // check to see if key is already set | |
| var word = words[j]; | |
| //if(word in result) | |
| if(!result.hasOwnProperty(word)) { | |
| // if not, set a key in result object with a value of | |
| // an object with a count key with value of 0 and a people | |
| // key with a value of array | |
| result[word] = { | |
| count: 0, | |
| people: [] | |
| }; | |
| } | |
| // increment counter | |
| result[word].count++; | |
| // check to see if person is in people array | |
| if(result[word].people.indexOf(person) == -1){ | |
| // if not add person to people array | |
| result[word].people.push(person); | |
| } | |
| } | |
| } | |
| } | |
| return result; | |
| } | |
| var result = wordCount({ | |
| "Ila Huels": [ | |
| "OPTIMIZE WEB-ENABLED SUPPLY-CHAINS", | |
| "brand sexy channels", | |
| "ENVISIONEER ROBUST E-COMMERCE", | |
| "TRANSFORM WIRELESS ARCHITECTURES", | |
| "BENCHMARK CROSS-PLATFORM PARTNERSHIPS" | |
| ], | |
| "Cristopher Feest": [ | |
| "BENCHMARK CROSS-PLATFORM PARTNERSHIPS", | |
| "brand sexy channels", | |
| "BENCHMARK 24/7 PARADIGMS" | |
| ] | |
| }); | |
| console.log(JSON.stringify(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment