loading words...

Jan 22, 2019 23:20:55

Frontend algorithms

by @swizecteller | 213 words | 🐣 | 116💌

Swizec Teller

Current day streak: 0🐣
Total posts: 116💌
Total words: 32303 (129 pages 📄)

Today I discovered that despite what anyone says, you still need _some_ algorithms even in frontend web development. It's not always just about managing emergent complexity of large systems. #200wordsTIL

I made this 👇

---

It's a little recursive algorithm that takes arbitrary JSON objects and prints them like this 👇

key: value

key:

   subKey: value

   ....

---

Quick and dirty UI so our sales reps can say "Yo, you gave me the wrong CVV code" instead of "Derp the card didn't go through"

Yes people do in fact share their credit cards over the phone.

---

Scary algorithm code! Total whiteboard stuff. Perfect little thing for a whiteboard interview actually 🤔

Here's how it works

---

Create a spacing. 4 spaces times current recursion level. Start at 0 for zero spaces.

Walk through object entries. They're key, value pairs.

For each entry, create a new line: `${spacing}${keys}`. Indents the key by amount of spacing.

---

If value is an object, break into new line and recurse. Pass the value and increment level. The method will return  perfect output for this entire object.

If value is string, append to line and continue iteration.

Return the string you've built.

---

For prettier strings I added a code to verbose text lookup.

---

And that's how basic algorithms came in handy today ✌️


Originally published at twitter.com

contact: email - twitter / Terms / Privacy