Interesting encounters in Javascript

Mehmet Egemen Albayrak

I tinker with javascript from time to time, sometimes solving others’ questions. This post contains interesting findings of my tinkering. It contains 4 examples you can enjoy. Very short and a concise post I think. Here the encounters:

Self-regulating function

One of the toptal interview questions was interesting. Question wanted me to find whether a string is a palindrome or not. For example “madam” is a palindrome, the word is the same when you start to write it backward. It was quite easy:

function isPalindrome(str) {
str = str.replace(/\W/g, '').toLowerCase();
return (str == str.split('').reverse().join(''));

There wasn’t any sanitization in my solution, to be honest. Question wanted us to do it under 160 characters. I thought of “how can I check a function is below 160 characters?” and then 5 seconds later I came up with:

`${isPalindrome}`.length < 160

So, if we want to have a self a self-regulating version of this code we can easily wrap the code in an if block. Unfortunately doing that makes it 169 characters.

Cracking the code

Another question comes from a pre-interview assignment. An array of numbers is given and said it contains the information for the next step. It looks something like this:

const mystArr = [104, 116, 116, 112, 58, 47, 47, 108, 111, 99, 97, 108, 104, 111, 115, 116, 47, 35, 119, 101, 65, 108, 114, 101, 97, 100, 121, 72, 105, 114, 101, 100]

Given that the outcome must be a string, I immediately understood that these are charCodes. I did this to decipher:

function decipher(arr) {
return String.fromCharCode(...arr);
decipher(mystArr); // returns "http://localhost/#weAlreadyHired"

Of course, the link wasn’t like that. That is what I would do. 😈

Side note, the code I wrote to generate the array is:

function make(str){
return str.split('').map(l => {
return l.charCodeAt(0)
make("http://localhost/#weAlreadyHired"); //returns mysterious array

Let it be

I write code quite practically. Once my friend told me, “writing code is like reinforcement learning”. I agree with him. One of the things I encounter is the problem in for loops which run delayed calls. Usual problematic code is:

for(var i = 0; i < 5; i++) {
}, i * 1000);

This code logs “5” to the console for 5 times after 1 to 5 seconds. 555 is the new devil. There are two ways to solve it. Either using “let” instead of “var” or wrapping the “setTimeOut” in an IIFE. Where did I encounter this problem? What made me learn two different solutions?

The first was an exact situation like the above code, a “setTimeOut” call in a “for” loop. The “let” solution happened in a script file which starts new Google Cloud Compute Engine instances to scrape the hell out of a website. It was something like this:

for(let i = 0; i < maxVmCount; i++) {
const compute = new Compute();
const zone ='us-central1-a');
const vm = zone.vm('vm-with-node-server-' + i);

Of course, I hit the Google Cloud max CPU limit. Immediately I sent a quota request to Google stating “I am making billions of calculations to unfold proteins for the good of society. I request 1000 CPUs”. Kidding, but I could, I am dangerous 🐱‍👤.

A different while

I was making a project called “WhereWereWe”. Basically, the idea sprung from my need to leave some comments to specific places on the posts that my friend writes. My first solution was to wrap every word in a clickable “span”. Of course the thought “this is not scalable” was instant. I was looking for a better solution and saw one, it was with “window.getSelection()”, getting the clicked index and slowly trying to figure out where is the end and start of the word by incrementing indices with a while function until it encounters with space. I learned this simple thing there:

const i = 0;while(i++ < someThresh) {
// logs 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

It is a way better way to increment index in a while loop. Probably you knew it, I didn’t.

I hope you enjoyed. Claps are not necessary, I always welcome comments as interactions. Please share your experiences too, I am sure you have some stories with minus zero. See you in a different post.

Mehmet Egemen Albayrak

Written by

Currently being creative

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade