Shakespearean Insult Generator

Was intimidated at first by coming off of the Codecademy platform and going local, but it was actually ok once I worked out what I wanted to do.

Took me 2 hours but could have been shorted as I went through using if statements at first and then realised that arrays make everything easier!

Because of this I put my arrays into my functions but should I have established them first outside of the functions?

Thank you!

1 Like

What you did is totally fine. You could dry your code a bit by creating the lists first, and having a single function to choose a random item from each list.

1 Like

Oh I see, yes that would be even faster! something like:

// Array1 // Array 2 // Array3 const createInsult =() => { let a = Math.floor(Math.random() * adjArray.length); let b = Math.floor(Math.random() * partArray.length); let c = Math.floor(Math.random() * nounArray.length) console.log(`Thou art ${adjArray[a]} ${partArray[b]} ${nounArray[c]}!`);

Thanks for your help!

1 Like

I played with your code a bit, and added a feature to choose a random number of participles to include. Got these insults,

Thou art a greasy, sodden-witted, sheep-biting hog!!! Get thee gone from my presence this instant!
Thou art a lecherous, tardy-gaited, idle-headed madman!!! Get thee gone from my presence this instant!
Thou art a starveling, three-inch, ill-breeding, fat-kidneyed whoreson!!! Get thee gone from my presence this instant!
Thou art a brain stick, worsted-stocking fustilarian!!! Get thee gone from my presence this instant!

Pretty funny stuff. Good job.

My code
const nounArray = ["bed-presser", "biscuit", "boar-pig", "bull's-pizzle", "carbuncle", "coward", "death-token", "eel-skin", "fustilarian", "harlot", "hog", "horseback-breaker", "knave", "madman", "neat's-tongue", "ratsbane", "rogue", "stock-fish", "whoreson", "villian", "worms-meat"];
const partArray = ["bat-fowling", "beetle-brained", "dismal-dreaming", "elvish-mark'd", "fat-kidneyed", "folly-fallen", "■■■■-hated", "idle-headed", "ill-breeding", "infant-like", "milk-livered", "reeling-ripe", "rude-growing", "sheep-biting", "sodden-witted", "swag-bellied", "tardy-gaited", "three-inch", "tickle-brained", "toad-spotted", "worsted-stocking"];
const adjArray = ["a bawdy", "a brain stick", "a fat", "a false", "a foul", "a greasy", "an infectious", "a kindless", "a lecherous", "a paunchy", "a puking", "a qualling", "a remorseless", "a rotten", "a sanguine", "a spherical", "a starveling", "a vile", "a venemous", "an ugly", "a yeasty"];

const getRandomNumber = upperLimit => Math.floor(Math.random() * upperLimit)

const createShakyInsult = () => {
    const adj = adjArray[getRandomNumber(adjArray.length)];
    const noun = nounArray[getRandomNumber(nounArray.length)];
    const parts = [];
    for(let i = 0; i < getRandomNumber(5) + 1; i++){
        parts.push(partArray.splice(getRandomNumber(partArray.length), 1)[0]);
    }
    return `Thou art ${adj}, ${parts.join(", ")} ${noun}!!! Get thee gone from my presence this instant!`;
}

console.log(createShakyInsult());

As far as drying your code goes, I had initially come up with the following, but there are many ways you can accomplish your task. I do love the insults.

code
const insultArrays = [adjArray, partArray, nounArray]
const getRandomNumber = upperLimit => Math.floor(Math.random() * upperLimit)

const createShakyInsult = () => {
    const insults = insultArrays.map(arr => arr[getRandomNumber(arr.length)])
    return `Thou art ${insults[0]}, ${insults[1]} ${insults[2]}!!! Get thee gone from my presence this instant!`;
}

console.log(createShakyInsult())
3 Likes