Mixed Messages project - How It All Ends

Hi all,

If you’re interested in my solution for the Mixed Messages project, here it is:

To answer the questions:

Any feedback, criticisms, suggestions are welcome.

1 Like

Very cool. There is an extra line at the beginning of the file that I do not see a purpose in having. It is preferred that variables whose value does not change are declared with const instead of let (generateMessage). Other than those 2 things (that wont affect the performance of the program) it looks great!

Refactoring is always the next step once we have proven our code runs properly.

const generateMessage = function () {
    const f = m => Math.floor(Math.random() * m)
    return `${f(times.length)}, ${f(causes.length)} will cause ${f(outcomes.length)}`
}

Three things to note:

  1. The outer function uses standard syntax.
  2. The critical code is shifted into a helper function.
  3. The template literal does all the assembly of data in desired return string.

Thanks for the suggestion - well spotted - I created that variable and didn’t use it. I’ve deleted it now.

That’s a very elegant solution. Very DRY :slight_smile:

I’ve refactored based on your approach - just went with a slight variation.

Thanks for taking the time to look over my code.

1 Like

Oh, one question though: when is it best to use the function() syntax rather than => ()?

I’m not really sure when to use one or the other… is there a generally accepted practice?

Thanks,
B

Simple, if you are using braces and return, then use
function and call a spade a spade.