This Javascript program randomly chooses from an array of phrases that each have placeholders for a noun, adverb, and verb. Each placeholder is parsed, iterated through to find the placeholders, which are then replaced with a randomly chosen element from its respective array. Joined back together, it creates a randomly generated ad lib.
I smell something off about this code but haven’t been able to figure out exactly what. Let me know if anything is redundant.
Thank you for checking out!
code link: https://github.com/OliviaJoyHanson/Javascript_Mixed_Messages_Project.git
This is a great start to you Mixed Messages project! I created something similar to yours, and my biggest struggle was adding the random words into random sentences. It’s always interesting to see how others reach the same end result! I’m still figuring out a better way to collect and implement the words for my final result.
If you want to continue with this project, perhaps some challenges you can look into are:
- How would you handle if someone wanted to add a sentence that actually uses the word “noun”, “verb” or “adverb”? Ex. The noun of this sentence is [insert random word].
- How would you handle singular/plural of nouns and tenses of verbs?
- How would you handle multiple nouns, verbs or adverbs in your phrase?
- When you have multiple nouns, verbs or adverbs, do you want any of them to repeat in the phrase?
On a side note, in randomNumber, your nums[Math.floor(Math.random() * nums.length)] is redundant since the array value is returning the same value as the index. You can just as easily write:
const randomNumber = num => Math.floor(Math.random() * num);
The num variable wouldn’t be necessary if the number would always be the same, but if you want flexibility, you can get your noun, for example, with words.nouns[randomNumber(words.nouns.length)];
Keep up the good work!