Random Sentence Maker Machine

Hi! This is my first project on Codecademy. When you click on a button, it makes a new random sentence. It’s helped me learn how to use different languages in a fun but real-life application and also how to use GitHub. I hope you enjoy it too!

My code is here: GitHub - JessRichT/MixedMessages: A random comment generator

Hey!

Nice work there!

It’s all very logical and tidy.
One thing you could do to make it even better is see where you’re re-using the same kind of code, and turn that into a function. So for example you’re running the same code Math.floor(Math.random()*(____.length)) on every array. The only thing that changes is the object you are .length-ing on.

So you could do something like this instead:

const getRandomisedIndex = (array) => Math.floor(Math.random()*(array.length));

Then you can use that function wherever you need it. For example when grabbing the word from the array:

const randomVerb = verb[getRandomisedIndex(verb)]; 

In this case you’re not saving that much code because you’re creating a new variable for each randomised selection. But if you were to make another function that does all that for you at the same time and returns the final output (instead of making variables for each random part), using the getRandomisedIndex() function would save a lot of work and code. :slightly_smiling_face:

Awesome work again!

Thank you for your very helpful advice! I’ve modified my code on GitHub by adding a function to calculate random indexes to make my code more efficient.

1 Like