Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mokey tiger haiku
var split = require('split');
var http = require('http');
function pullHaikuFromLine(line, offset) {
return line.replace(/\s{2,}/g, ' ')
.replace(/<div class="line">/g, "\n")
.replace(/<(?:.|\n)*?>/gm, ' ')
.substring(3);
}
function handleGetHaiku(cb) {
http.get('http://www.randomhaiku.com/', function(res) {
res.pipe(split()).on('data', function (line) {
if (line.indexOf('div class="poem"') >= 0)
cb(pullHaikuFromLine(line, 0));
});
}).on('error', function(e) {
process.stderr.write('error in stream D: ' + e.message);
});
}
function hasTiger(haiku) {
var lHaiku = haiku.toLowerCase();
return lHaiku.indexOf('t') >= 0 &&
lHaiku.indexOf('i') >= 0 &&
lHaiku.indexOf('g') >= 0 &&
lHaiku.indexOf('e') >= 0 &&
lHaiku.indexOf('r') >= 0;
}
function hasMonkey(haiku) {
var lHaiku = haiku.toLowerCase();
return lHaiku.indexOf('m') >= 0 &&
lHaiku.indexOf('o') >= 0 &&
lHaiku.indexOf('n') >= 0 &&
lHaiku.indexOf('k') >= 0 &&
lHaiku.indexOf('e') >= 0 &&
lHaiku.indexOf('y') >= 0;
}
function handleSearchHaiku(haiku) {
console.log(haiku);
var tiger = hasTiger(haiku);
var monkey = hasMonkey(haiku);
if (tiger && monkey)
console.log('TIGER MOKEY!!! :D');
else if (tiger)
console.log('TIGER!');
else if (monkey)
console.log('MONEKY!');
else
console.log('NO TIGER, NO MONKEY :[');
}
handleGetHaiku(handleSearchHaiku);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.