-
-
Save jamesflorentino/9149594 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
node_modules |
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
require('http').get('http://www.reddit.com/r/showerthoughts.json', function(res) { | |
var body = ''; | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
var data; | |
try { | |
data = JSON.parse(body); | |
} catch(err) { | |
throw err; | |
} | |
var children = data.data.children; | |
var total = children.length; | |
var randomItem = children[Math.round(Math.random() * (total - 1))]; | |
console.log(randomItem.data.title); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment