I'm back in the bay!
Well, it's definitely been a while since I've blogged, but I'm back! Back to blogging, and back in San Francisco (where I grew up). While the city has certainly changed a lot since I was a young thing in high school, at least the Richmond district hasn't undergone a revolution (at least not one that I can see yet). I'm writing in the old café Zephyr, now known as La Promenade Cafe, but despite the French name, the food is still mediocre and the ambiance is still wonderful. Before I moved to SF I took a month off to travel around Israel, Jordan and Germany.
Beach in Tel Aviv
Petra in Jordan
Church in Berlin
The places may seem random, but that's where my international friends are these days, and I'm a consummate cheapskate and hate paying for hotels. Then I had thanksgiving with the fam in Arizona. Suffice it say, after all this travel, I'm very happy to be in one place for a while.
In my free time I've been going back to JavaScript basics, and reading Eloquent JavaScript. I like the book a lot, and it does an excellent job of explaining some JavaScript nuances. I'm also moving my BCP project over to Rails, so keep tuned for updates!
Back to EloquentJS. One of the problems was how to flatten an array.
The first thing I had to think about was how to use "reduce." According to MDN, "The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) has to reduce it to a single value." Ooof what does that mean!
Reduce, similar to forEach, takes an array and iterates through it, performing some sort of function on the current value and then returns it. So, it can be used to find and return a minimum value in an array.
Here is my solution:
The first thing I am doing is calling reduce on the variable arrays (which was defined in the problem). Reduce then takes a callback function with three arguments: the previous value in the array, the current value in the array, and the index. The if statement ensures that reduce will not try to evaluate an undefined value, which will cause an error.
Then, the newArray variable is the concatenated array that will be returned.
And there you go!












