This is my mixed messages generator project. Could it be better?

I really enjoyed this project. But I think it can be better. What do you think?

That’s great!

One practical adjustment to make is to not have the function depend on outside factors. Here there’s an assumption the global variables are going to be available for the function. If you make the function take in the expected input it’s going to work with (in this case the arrays), you make it more re-usable in the future.

Then there’s the added nuance of how you would pass. Javascript has the default of arrays and objects by reference. Notice this behavior.

function maybeModify(arr) {
  arr[0] = 100;
  return arr;
let myArray = [1,2,3,4];
let newArray = maybeModify(myArray);
console.log(myArray); // [100,2,3,4]... ew both items modified

function dontModify(arr) {
  deepCopy = [...arr];
  deepCopy[0] = 100;
  return deepCopy;

myArray = [1,2,3,4];
newArray = dontModify(myArray);
console.log(myArray); // [1,2,3,4]... cleaner, only newArray is changed

The caveat with keeping the function “pure” (without side-effects) is the extra cost of copying. Maybe in C++ we might pass by reference more often (or const ref), but not in javascript.