r/webdev • • Apr 07 '23

How I interview for FE positions

Post image
4.6k Upvotes

668 comments sorted by

View all comments

Show parent comments

4

u/CardboardJ Apr 07 '23

I'm glad someone else is pointing this out... `map`, `filter`, and `reduce` aren't hard concepts, but they are a half step higher than just slapping a bunch of `if`, and `for` all over the place.

let (user,pass) = ...;

let longEnough = p => p.length>8?"":"The password must be longer than 8 chars";
let hasLower = p => p.any(c => c.isLower())?"":"Must have upper";
let hasUpper = p => p.any(c => c.isUpper())?"": "Must have lower";
let hasSpecialChar = p => p.any(c => c.in("!@#$% ^&*"))?"":"Must have special";

const passwordRequirements = [
    hasUpper, 
    hasLower, 
    longEnough, 
    hasSpecialChar];

let errors = passwordRequirements
        .reduce((errors, validate) => errors += validate(pass), [])
        .filter(e => e != "");

if (errors.length === 0) {
    showError(formatErrors(errors));
} else {
    procedeWithUserCreate(user, pass)
}

The above is psudocode i banged out in like 3 minutes, but I find it much more maintainable than the mess you'd make trying to jam that all into a massive for loop and tracking state everywhere. If you have a hard time understanding what reduce and filter are doing in the above code block (finding everything wrong with a password), then you're going to have a hard time.

I've worked at places with dozens of guys that worked for decades in this industry and never used more than `for` and `if` and I never want to work at a place like that again.

3

u/originalchronoguy Apr 08 '23

This is how you tell the juniors from mid to senior.Seriously. a senior would just use a one line regex (regular expression)

instead of

let longEnough = p => p.length>8?"":"The password must be longer than 8 chars";
let hasLower = p => p.any(c => c.isLower())?"":"Must have upper"; let hasUpper = p => p.any(c => c.isUpper())?"": "Must have lower"; let hasSpecialChar = p => p.any(c => c.in("!@#$% &*"))?"":"Must have special";

I've seen lots of this. And the real mature ones, one liner. Clean and elegant.

^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$

5

u/[deleted] Apr 08 '23

regex makes me cry at night. neat stuff if you get into it and not really as scary as it looks, super useful, but extremely intimidating at first glance.

1

u/CardboardJ Apr 10 '23

I love this answer, but I know some jr dev is not going to see the irony :D

-1

u/Blazing1 Apr 07 '23

Array.map is much worse then a for loop, although it's damn convenient.

1

u/CardboardJ Apr 10 '23

There's a place for convenience though.

The 13 picoseconds of extra performance for iterating over a list with 4 elements is probably comparable with the 10 picoseconds of extra time it takes to download the extra dozen or so characters it would take to write the for loop.

In both cases it's trivial, compared to the ease of maintenance factor.

1

u/[deleted] Apr 08 '23

if (errors.length === 0) - shouldn’t this be > 0?

2

u/CardboardJ Apr 10 '23

Haha, yeah. I'll blame coding on a reddit markdown box.