Mixin' Messages - Silly Strings

I found this project very easy but also fun. I’d like more of a challenge but it was also kind of a nice breather. I seem to have taken a much different approach to most people here… I’d definitely love some feedback! Alternatively, if you want to suggest a challenge you’d like me to take a crack at relating to this project please do!

I almost tried to code something which would make words plural or past-tense when appropriate but instead I decided to keep what little sanity I have left.

Here’s a link to the code on GitHub!

Edit: I guess you could also just run it here if you want to see a string or two :slight_smile:

//Takes: an array of any type //Returns: a random value from that array function randFromArray(inputArray){ const l = inputArray.length; if (l === 1) return inputArray[1]; //Error checking, not necessary // if (l === -1) { // console.log('ERROR: ARRAY IS EMPTY'); // return 'ERROR'; // } const randIndex = Math.floor(Math.random() * l); return inputArray[randIndex]; } //Returns: a random message made of random strings function getRandomMsg(){ // #region Word definitions const msgNouns = [ 'shrimp', 'dog', 'cat', 'soup', 'water tower', 'girl', 'guy', 'boy', 'essence', 'vibe', 'treasure', 'magician', 'orc', 'moon', 'earth', 'planet', 'sun', 'star', 'pal', 'friend', 'bug', 'shoe', 'shirt', 'service', 'elf', 'scientist', 'coffee' ]; const msgAdjectives = [ 'rare', 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'legendary', 'exceptional', 'unique', 'shabby', 'creepy', 'wet', 'sick', 'cool', 'hot', 'big', 'small', 'black', 'grey', 'white', 'capable', 'gluten free', 'strange', 'low carb', 'credible', 'shway', 'girly', 'manly', 'taken', 'stinky', 'special', 'mundane' ]; const msgVerbs = [ 'eat', 'buy', 'create', 'jump', 'run', 'attack', 'roll', 'shoot', 'pick', 'cook', 'bury', 'vaporize', 'smoke', 'drink', 'love', 'alienate', 'groove', 'dance', 'explode' ]; // #endregion const aNoun = () => { return randFromArray(msgNouns) }; const anAdj = () => { return randFromArray(msgAdjectives) }; const aVerb = () => { return randFromArray(msgVerbs) }; const msgStructures = [ `I ${anAdj()} ${aVerb()} the ${aNoun()}.`, `The ${anAdj()} ${aNoun()} ${aVerb()}ed.`, `Going to the ${anAdj()} store to ${aVerb()} some ${aNoun()}.`, `Is this ${aNoun()} ${anAdj()}? I'd like to ${aVerb()} it.`, `This one goes out to all the ${anAdj()} ${aNoun()}s out there... keep on ${aVerb()}ing!`, `Yeah... ${aNoun()}... ${aNoun()}... ${aNoun()}... they're all the same...`, `Before you can ${aVerb()}, the ${aNoun()} ${aVerb()}s!`, `Would the ${aNoun()} like a ${anAdj()} ${aNoun()} ${aNoun()}?` ]; const finalMsg = randFromArray(msgStructures); return finalMsg; } //--- console.log(getRandomMsg());

Hi, I’ve looked over your code and looks pretty good. The creation and of the randFromArray() function was a good idea since you were able to reuse it for different parts of the program. The problem is relatively simple so there isn’t much that could go wrong(or has gone wrong in your case), but overall it was an elegant solution. I also noticed the function comments which were interesting. For a small project like this one it may not matter much but going forward you may want to put comments that explain “why” certain things were done the way they were.

For a challenge: A different approach might also be to load the words from a text file into a list. While having them already in a list slightly improves speed, it sacrifices your ability to easily use thousands of words and giving your program some more ‘umph’ ( =) ) as a result. Also loading from a file gives you more flexibility when it comes to adding and deleting words.Moreover, you probably will not notice any difference in speed between your solution and the alternate solution. → Note that you may have to do some checking on the list to make sure it contains words.

Once again, excellent job with your solution.

1 Like