Zachary Forrest y Salazar

Carl Solomon! I am with you in Rockland.

Countable.js ⇛

This is probably the nicest word/character/paragraph counter that I’ve come across. The code is exquisite.

Rickshaw

Rickshaw by Banksy

If I ever have an opportunity to own a Banksy, I’m going to take it.

Unique vs. Duplicate

I’ve been working to be more comfortable with native JavaScript instead of relying on a library for everything. Most of this has been triggered by the fact that I don’t seem to interview well, and while I’m not convinced that the questions we ask engineering candidates are completely stupid, the fact remains that I could be better at what I do, both academically and practically.

And nothing beats practice.

So today’s problem deals with getting unique and duplicate values from an Array in JavaScript. We’re really dealing with just two sides of the same coin. I’ve documented the code quite extensively below, but let me know if you have any questions.

You Are A Unique Snowflake

Not too bad. What we’re doing is keeping track of which values are unique by using a hash table. We loop through the values, and if the value doesn’t exist in the hash table, we push it to our return array, as well as our hash as a key with a value of 1.

We do this so that if the value is duplicated, it doesn’t get added to our return array again. That’s where the hasOwnProperty() method becomes useful.

You Are A Duplicate Snowflake

So how does this work? The same patterns that we used in the unique() function can be applied here. We have a hash table to keep track of the values that we’ve seen. In this case however, we need to keep track of how many times we’ve seen a particular value. That’s what the first loop does.

With the second loop, we make sure the properties of our hash table belong to the object (and not further up the prototype chain) and whether we’ve seen that property more than one time or not. If we have, we push it to our return array.

Have improvements? Suggestions? Let me know on Twitter.

Odds & Evens

Let’s say you want to take an array of integers, and sort them by odds and evens. So for example:

1
[3, -3, 0, 24, 91, 105, 2220, 7]

should become

1
[0, 24, 2220, -3, 3, 7, 91, 105]

And yes, zero is an even number.1

How would we do that?

Stack Overflow to the rescue.

1
2
3
4
5
[3, -3, 0, 24, 91, 105, 2220, 7].sort(evensAndOdds);

function evensAndOdds(a, b) {
    return Math.abs(a % 2) - Math.abs(b % 2) || a - b;
}

The modulus determines if the number is odd or even and groups them. The Math.abs() method lets us to sort negative integers as well as positive. And finally, our a – b OR condition allows us to sort the grouped even and odd numbers according to ascending values.

Magic.

So let’s modify the function to output odds before evens.

1
2
3
function oddsAndEvens(a, b) {
    return Math.abs(b % 2) - Math.abs(a % 2) || a - b;
}

We can also modify the function to return values in descending order.

1
2
3
function oddsAndEvens(a, b) {
    return Math.abs(b % 2) - Math.abs(a % 2) || b - a;
}

Bonus

There is a bitwise version of this sorting function as well, but I’m having a hard time understanding bitwise operators.

1
2
3
function oddsAndEvens(a, b) {
    return (a & 1) - (b & 1) || a - b;
}

Have an explanation? Ping me on Twitter.

Fibonacci

I was asked during an interview once, to write a function (with pen and paper) that took an integer and returned that integer’s place in the Fibonacci sequence.

There was no way in hell that I was going to be able to write that function with pen and paper. I’m just not that guy. But for my own edification, I decided to write the function later and more importantly, keep this sort of thing around. It’s good practice.

Have improvements? Suggestions? Let me know on Twitter.