Skip to content

Instantly share code, notes, and snippets.

@bgoonz
Created August 18, 2021 22:10
Show Gist options
  • Save bgoonz/8f5b8523670ec6b4fc7b6b4111d0c68a to your computer and use it in GitHub Desktop.
Save bgoonz/8f5b8523670ec6b4fc7b6b4111d0c68a to your computer and use it in GitHub Desktop.
madlibs
#*2 digit number*#% of the internet #*noun*#, or about #*2 digit number*# million #*noun*#,
said they went #*adjective*# to get news or #*noun*# about the #*YEAR*# elections. We
call them #*adjective*# political #*noun*# consumers.
#*2 digit number*#% of the internet #*noun*#, or about #*2 digit number*# million #*noun*#,
said they went #*adjective*# to get news or #*noun*# about the #*YEAR*# elections. We
call them #*adjective*# political #*noun*# consumers.
#*2 digit number*#% of #*noun*# users, or about #*2 digit number*# million people, said they
used #*adjective*# to discuss #*noun*#, and one of the most #*adjective*# #*noun*# was jokes
about the #*noun*# and the election. #*2 digit number*# of #*noun*#, or more than #*2 digit number*#
million people, #*verb*# online to #*verb*# directly in #*adjective*# activities such as donating
#*noun*#, volunteering, or #*gerund: #####ing*# about #*adjective*# events to attend.
I think President #*last name*# is a #*adjective*# #*noun*# and should be #*verb: past tense*# in the #*noun*#.
/*
1) fs.readfile base madlib
2) search for ()
3) prompt user for word within ()
4) replace that word including the ()
5) repeat for every case of ()
6) console.log the result
7) fs.writefile to a new .txt file
Bonus objectives:
1) Randomize the given madlib
2) Prompt user "Are you ready for a MadLibs? (yes/no)" -> if they respond "Adult" an adult Madlibs is given
3) Keep the adult madlibs in a hidden directory
*/
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const getNextWord = (text) => {
if (text.includes('#*')) {
let openingIndex = text.indexOf('#*');
let closingIndex = text.indexOf('*#');
let word = text.slice(openingIndex + 2, closingIndex)
return [word, openingIndex, closingIndex];
}
}
const replaceWord = (text, nextWordArr) => {
let [replace, openingIndex, closingIndex] = nextWordArr // Fix: WORD is NOT Replace
let before = text.slice(0, openingIndex);
let after = text.slice(closingIndex + 2);
return before + replace + after;
}
fs.readFile('madLibShort.txt', 'utf8', (err, data) => {
if (err) {
console.log(err);
return
}
const getFindReplace = (longStr) => {
// base case
if (longStr.indexOf('#*') === -1) {
rl.close();
console.log(longStr);
fs.writeFile(`completed_madLibs.txt`, 'madLibs.js', 'utf8', err => {
if (err) {
console.log(err);
return
}
});
return longStr
}
let nextWordArr = getNextWord(longStr);
let nextWord = nextWordArr[0];
rl.question(`Please choose a ${nextWord}: `, (answer) => {
nextWordArr[0] = answer;
let alteredText = replaceWord(longStr, nextWordArr);
return getFindReplace(alteredText)
});
}
return getFindReplace(data)
});
/*
1) fs.readfile base madlib
2) search for ()
3) prompt user for word within ()
4) replace that word including the ()
5) repeat for every case of ()
6) console.log the result
7) fs.writefile to a new .txt file
Bonus objectives:
1) Randomize the given madlib
2) Prompt user "Are you ready for a MadLibs? (yes/no)" -> if they respond "Adult" an adult Madlibs is given
3) Keep the adult madlibs in a hidden directory
*/
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const getNextWord = (text) => {
if (text.includes('#*')) {
let openingIndex = text.indexOf('#*');
let closingIndex = text.indexOf('*#');
let word = text.slice(openingIndex + 2, closingIndex)
return [word, openingIndex, closingIndex];
}
}
const replaceWord = (text, nextWordArr) => {
let [replace, openingIndex, closingIndex] = nextWordArr // Fix: WORD is NOT Replace
let before = text.slice(0, openingIndex);
let after = text.slice(closingIndex + 2);
return before + replace + after;
}
fs.readFile('madLibShort.txt', 'utf8', (err, data) => {
if (err) {
console.log(err);
return
}
const getFindReplace = (longStr) => {
// base case
if (longStr.indexOf('#*') === -1) {
rl.close();
console.log(longStr);
fs.writeFile(`completed_madLibs.txt`, 'madLibs.js', 'utf8', err => {
if (err) {
console.log(err);
return
}
});
return longStr
}
let nextWordArr = getNextWord(longStr);
let nextWord = nextWordArr[0];
rl.question(`Please choose a ${nextWord}: `, (answer) => {
nextWordArr[0] = answer;
let alteredText = replaceWord(longStr, nextWordArr);
return getFindReplace(alteredText)
});
}
return getFindReplace(data)
});
#*2 digit number*#% of the internet #*noun*#, or about #*2 digit number*# million #*noun*#,
said they went #*adjective*# to get news or #*noun*# about the #*YEAR*# elections. We
call them #*adjective*# political #*noun*# consumers.
#*2 digit number*#% of the internet #*noun*#, or about #*2 digit number*# million #*noun*#,
said they went #*adjective*# to get news or #*noun*# about the #*YEAR*# elections. We
call them #*adjective*# political #*noun*# consumers.
#*2 digit number*#% of #*noun*# users, or about #*2 digit number*# million people, said they
used #*adjective*# to discuss #*noun*#, and one of the most #*adjective*# #*noun*# was jokes
about the #*noun*# and the election. #*2 digit number*# of #*noun*#, or more than #*2 digit number*#
million people, #*verb*# online to #*verb*# directly in #*adjective*# activities such as donating
#*noun*#, volunteering, or #*gerund: #####ing*# about #*adjective*# events to attend.
I think President #*last name*# is a #*adjective*# #*noun*# and should be #*verb: past tense*# in the #*noun*#.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment