Get Programming with JavaScript - Listing 10.06
Listing 10.06 - Counting words from tweets
JS Bin
var tweets = [
"Education is showing business the way by using technology to share information. How do we so so safely?",
"Enjoy a free muffin & coffee with Post Plus, our new loyalty club exclusive to subscribers!",
"We're LIVE on Periscope right now answering all your #pet questions - tweet us yours now!",
"Saw an unfinished print of M:I5 yesterday and it is my great pleasure to report it is rather splendid.",
"Downloading the beta of OS X El Capitan. This is going to gooch my mac for sure, but it will probably look pretty",
"Mud pies and muscle: H.G. Wells weighs in on #STEMeducation in an automated age",
"UK Space Agency brings astronaut’s mission to children across the UK #ESA #ESERO",
"Trying to find an old tweet that I didn't favourite at the time is…a fools game. #continuesDigging",
"A robot has just passed a classic test of self-awareness. What does that mean?",
"It was a great event. Very important to learn how to communicate about science and engineering effectively"
];
var words = {};
var tweetText = tweets.join(" ");
var tweetWords = tweetText.split(" ");
tweetWords.forEach(function (word) {
words[word] = 0;
});
tweetWords.forEach(function (word) {
words[word] = words[word] + 1;
});
console.log(words);
Further Adventures
Listing 10.06 - Counting words from tweets - Task 1
- Add more tweets to the tweets array and run the program again.(They don't have to be tweets, any text will do.)
var tweets = [
"Education is showing business the way by using technology to share information. How do we so so safely?",
"Enjoy a free muffin & coffee with Post Plus, our new loyalty club exclusive to subscribers!",
"We're LIVE on Periscope right now answering all your #pet questions - tweet us yours now!",
"Saw an unfinished print of M:I5 yesterday and it is my great pleasure to report it is rather splendid.",
"Downloading the beta of OS X El Capitan. This is going to gooch my mac for sure, but it will probably look pretty",
"Mud pies and muscle: H.G. Wells weighs in on #STEMeducation in an automated age",
"UK Space Agency brings astronaut’s mission to children across the UK #ESA #ESERO",
"Trying to find an old tweet that I didn't favourite at the time is…a fools game. #continuesDigging",
"A robot has just passed a classic test of self-awareness. What does that mean?",
"It was a great event. Very important to learn how to communicate about science and engineering effectively",
"1794 William Whewell, born, UK scientist,known for his survey of the scientific method & creating scientific words.",
"Today on Drive Time is Ken Branagh Day (featuring Ken Branagh in the titular role). A marvellous thing indeed.",
"Children do better with no shoes? Think how well they'd do with feet!",
"Teachers claim to give students feedback on their work, but students say that this is not what they experience."
];
var words = {};
var tweetText = tweets.join(" ");
var tweetWords = tweetText.split(" ");
tweetWords.forEach(function (word) {
words[word] = 0;
});
tweetWords.forEach(function (word) {
words[word] = words[word] + 1;
});
console.log(words);
Listing 10.06 - Counting words from tweets - Task 2
- Change the keys in the square brackets to use word.toLowerCase() instead of word. Make sure you change them all.
var tweets = [
"Education is showing business the way by using technology to share information. How do we so so safely?",
"Enjoy a free muffin & coffee with Post Plus, our new loyalty club exclusive to subscribers!",
"We're LIVE on Periscope right now answering all your #pet questions - tweet us yours now!",
"Saw an unfinished print of M:I5 yesterday and it is my great pleasure to report it is rather splendid.",
"Downloading the beta of OS X El Capitan. This is going to gooch my mac for sure, but it will probably look pretty",
"Mud pies and muscle: H.G. Wells weighs in on #STEMeducation in an automated age",
"UK Space Agency brings astronaut’s mission to children across the UK #ESA #ESERO",
"Trying to find an old tweet that I didn't favourite at the time is…a fools game. #continuesDigging",
"A robot has just passed a classic test of self-awareness. What does that mean?",
"It was a great event. Very important to learn how to communicate about science and engineering effectively",
"1794 William Whewell, born, UK scientist,known for his survey of the scientific method & creating scientific words.",
"Today on Drive Time is Ken Branagh Day (featuring Ken Branagh in the titular role). A marvellous thing indeed.",
"Children do better with no shoes? Think how well they'd do with feet!",
"Teachers claim to give students feedback on their work, but students say that this is not what they experience."
];
var words = {};
var tweetText = tweets.join(" ");
var tweetWords = tweetText.split(" ");
tweetWords.forEach(function (word) {
words[word.toLowerCase()] = 0;
});
tweetWords.forEach(function (word) {
words[word.toLowerCase()] = words[word.toLowerCase()] + 1;
});
console.log(words);