Mixed Messages - Random Character Name, Sarcasm, Inspirational Quote

A short JavaScript program, under 70 lines of code, that spits out a random character name, a sarcastic message, and an inspirational quote onto the terminal.

Check out my version of Mixed Messages here.

Thank you very much for taking your time in checking out my repo, much appreciated :slight_smile:

1 Like

Nicely done :slight_smile:

I only find this:

  const getName = messages.names[randomIndex(messages.names.length)];
  const getSarcasm = messages.sarcasm[randomIndex(messages.sarcasm.length)];
  const getInspiration =

some what repetitive. So either would change the randomIndex function to give me a random element, or just add another function which gets the random element for you (which uses the random index function)

1 Like

Perfect! Iโ€™ll implement that right away, thank you!

1 Like

I gave you two options here:

  1. change the randomIndex function (to give you a random element)
  2. add a function which gives the random element using the random index function

you should take a moment to think about which implementation you want and why :slight_smile:

1 Like

The first one is easier and the second one, Iโ€™m assuming, is essentially using a callback function cause you are proving the return value of randomIndex function into another function, isnโ€™t it?

This is an example from a callback function:

function greeting(name) {
  alert('Hello ' + name);

function processUserInput(callback) {
  var name = prompt('Please enter your name.');


where we pass the function as argument (last line)

I was thinking simpler along the lines of:

const randomElement = () => {

where you simple call randomIndex from inside randomElement.

callback would be overkill I think

1 Like

Ah, I see!

This is indeed a better alternate, let me give it a shot!

Not sure if I did it correctly, but I added that extra function you were talking about to return the element.

I think its an improvement and was a good challenge for you :slight_smile: How do you feel about it?

Not gonna lie, I felt pretty good when the new code was working. :sweat_smile:

1 Like