Mixed Messages: Pun Generator!

Hey fellow learners!

Here’s my take on this project. It took me about 1 hr to complete and refine/refactor. The most difficult part for me was figuring out how to format an unformatted list of puns from a random website (there were a lot) and how to import/export them to the main app.js file. To format the list, I used VS Code’s multi-cursor capabilities to remove extraneous characters, enclose the puns in " symbols and end each pun in the array with a “,”.

I wanted to make a pun message generator that starts with an opening phrase, serves up a delicious pun, then ends with a closing phrase.

While I did use Git, I opted not to use GitHub, so no repo. However, since my codebase is small anyways, here it is:

let punObj = require('./puns.js');

let punArr = punObj.array;

let openerArr = [
    "May I present you with a pun?",
    "Would a pun lighten the mood?",
    "How about pun for the road?",
    "Here is your pun, sir/madam."
];

let closerArr = [
    "Did you enjoy that as much as I did?",
    "I don't know about you, but I'm spent.",
    "Well, that was one for the ages!",
    "I know, I know. You've heard it before."
];

let generateRandomNum = n => Math.floor(Math.random()*n)

let selectRandomMessageComponent = array => array[generateRandomNum(array.length)];

let createMessage = () => {
    let message = []
    message.push(selectRandomMessageComponent(openerArr));
    message.push(selectRandomMessageComponent(punArr));
    message.push(selectRandomMessageComponent(closerArr));
    return message.join('\n');
};

console.log(createMessage())

The list of puns (over 100+) was exported from puns.js using the exports.array syntax.

Expected output is: Opener\nPun\nCloser

In terms of what I’d love to do if I was revisiting this in the future, I’d definitely make a quick one-page app where you press a button and get the message. I’d also refactor the createMessage function to be a for loop or some kind of array iterator method in case I wanted to scale how many message components were part of each message.

All feedback appreciated.

Heyo,

For anyone interested, I refactored my code to be even more readable and modular.

So in order to loop through all the arrays I’d give it, I had to store the array of arrays in a variable, which I called “arrList”.
Then, I created a for loop to iterate through arrList and push all randomly selected message components to the message array inside of createMessage().
However, I realized I could refactor further if I used the .map() method instead.
Then, since you can chain methods, I realized that I didn’t have to store the message in an array and then return it. I could just return the .join()'d array created by mapping through arrList.
All this allowed me to refactor the createMessage() function into a single-line concise-body.

//Imports then stores the array in puns.js to punArr
let punObj = require('./puns.js');
let punArr = punObj.array;

let openerArr = [
    "May I present you with a pun?",
    "Would a pun lighten the mood?",
    "How about pun for the road?",
    "Here is your pun, sir/madam."
];

let closerArr = [
    "Did you enjoy that as much as I did?",
    "I don't know about you, but I'm spent.",
    "Well, that was one for the ages!",
    "I know, I know. You've heard it before."
];

//Stores list of arrays in arrList
let arrList = [openerArr, punArr, closerArr]

let generateRandomNum = n => Math.floor(Math.random()*n)

let selectRandomMessageComponent = array => array[generateRandomNum(array.length)];

/* Returns the joined result of calling selectRandomMessageComponent on all arrays
in arrList */
let createMessage = () => arrList.map(arr => selectRandomMessageComponent(arr)).join('\n');

console.log(createMessage())
2 Likes

What a great idea! I really like that you are using puns from a random website.
Your code looks very clean and organized! I like your comments on this second posting. I did not see this at first and was trying to understand your code from your first posting.
I have never worked with importing/exporting elements from a website to my js file. I’m curious to see how you did that.
Is that code mainly in the puns.js file?
Greetings,
Julia

Hi Julia,

The extra code required in the puns.js file to accomplish this is:

exports.array = ['pun1', 'pun2', ...]

I’m not sure that this is the best method to do this, but it was the one I could make work the fastest.

To clarify, I wasn’t importing the array from the website. I had to copy+paste the contents of the list on the website into an empty .js file on my machine and then format it manually. This was made easier because VS Code allows you to select and edit multiple lines at a time.

Oh I see, thank you for clarifying! This makes me want to try something similar soon. :slight_smile:

1 Like