Originally posted on my blog
For the last several weeks I've been applying for jobs. Sadly, the startup I was working at just didn't work out. I...
For further actions, you may consider blocking this person and/or reporting abuse
Hey,
Great job from your end. I prefer functional programming.Maybe because I have a Maths background and I dread for loops😉
One advice to make it easier for you to remember and also make your code more readable.
For example your filter should look something like this:
const evenNumbers = numbers.filter(number => number %2 === 0)
Just naming your variable correctly reduces the confusion.
One, you know the filter is returning even numbers. Also, it will quickly stick that you are just iterating over
numbers
and checking if eachnumber
is even. That way that variable wouldn't look made up. It will make perfect sense.Best wishes.
Excellent contribution! I actually like to have this in hand, to remind me of that kind of stuff.
cheatography.com/costemaxime/cheat...
Very good input. I have a single critique, though, and this is ONLY because you're prepping: with reduce. Me, as a hiring person, I question deeper when candidates don't provide the third init value.
I know it's arguably pointless, especially when implicit inits come into play, but that's how it is. And you'll come across others who think the same.
To reiterate, this is gold. But those little things... A lot of times, hiring people will grab onto them and dig deeper. (Probably not what you want for a smooth-as-butter interview lol)
Interesting, I’d love to learn more, could you expound with an example?
I was on mobile last night and I've been busy today, so I honestly didn't expect to have a reply. ...but whaddaya know, I JUST NOW used a reduce lol.
The init value - technically - protects you from a
TypeError
if you encounter an empty array. init, if not provided, is implicitly taken fromarr[0]
. If!arr[0]
... Muerte.Here, I'm writing a cypress e2e test that finds all the card rows in the page and guarantees the contained cards are the same height. To do so, I sum the heights of all the cards, divide that by cards.length, and compare the final avg to
cards[0]
. (a variance inside will break thecards[0]===avg
, I hope :D)Not such a problem with Cypress, but it could be possible that there are no cards in the row.
Most uses of reduce aren't for summing, though. Its return - more often than not - is a complex object, rather than a simple value. In these cases:
arr[0]
can't properly init the product, because product and items have very different structures.Very cool! Thanks for sharing this example.
I still dont get the .reduce() 😔
Since you understand map but don't understand reduce, implementing map with reduce might help:
Edit: here's the same for filter:
That’s ok! It took me a long time to get it. Reduce is useful because you get a single value returned from an array of values. For example, if you have an array and you need to total of all the numbers.
The accumulator is just the total up to that point and the currentValue is just the value at whatever index you are currently at. So if you are adding, it’ll be the total + the value and then repeat until it’s gone through every number in the array and return the value of the last operation. Hope this helps! Try playing with the MDN examples they may help too!
Hi Andy, I'm a newbie to JS so first of all, thanks for your contribution. Very useful stuff in here!
A common problem I encounter when reading documentation on JS is figuring out what words are actual keywords and what are the made up stuff.
Do you have any other example like the map() method?
Hey! I’ve seen this but haven’t had time to think of another example. Check the MDN docs linked in the article and you’ll find more!
Hey! Nice article!
I have a repository with some examples, if anyone is interested, check it out: github.com/pedroapfilho/array-methods
Hey there! I shared your article here t.me/theprogrammersclub and check out the group if you haven't already!
Thanks Yogeswaran!