This Week in JavaScript - 7 October 2013

This one reads odd to me. First, an array is not recursive by itself.

I’m not so sure what you mean by values passed byRef: primitive values inside the array or the array itself. All I know is that in Javascript anything other than primitive values is passed by reference.

Then, you probably heard that (almost) everything is an object in Javascript. Arrays in Javascript are high-level, list-like objects (Array - Javascript | MDN). To pass an array by value you can .slice() it, although non-primitive values in it will still be passed by reference in the new array.

Array.prototype.slice - Javascript | MDN

slice does not alter the original array, but returns a new “one level deep” copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:

For object references (and not the actual object), slice copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.

For strings and numbers (not String and Number objects), slice copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other array.

If a new element is added to either array, the other array is not affected.

I stumbled on an interesting article today that uses reduce() to solve question #4. It specifically references this quiz. :slight_smile:

function findLongest(entries) {
  return entries.reduce(function (longest, entry) {
    return entry.length > longest.length ? entry : longest;
  }, '');
}

I just realize that I was going about it the wrong way and so is he and so the all of us. What if there is more than one longest string? The solution is a filter, not a max nor a fold (reduce).