A Guide to Understanding Binary Search

In working with collections (lists, queues, maps) of data, you usually have to perform one or more of the following operations on the collection: accessing, searching, appending, inserting, replacing/swapping, deleting and sorting. Binary search is one of the most popular algorithms for searching for a number within a sorted list of numbers. It has a time complexity of O(log N) which means that its ability to find a number in a sorted list is minimally affected by the length of numbers in the list. ...

June 1, 2024 · 7 min · Orim Dominic Adah
URL validation JavaScript header image

How I Validated for Specific URLs in JavaScript

In a pet project that I worked on recently, one of the requirements was to allow users to submit the URL to their Facebook social media profile. This article describes how I ensured that only Facebook profile URLs were submitted. JavaScript was used but it does not matter as much as the algorithm used; and that algorithm is described here. function validateUrl(url, expectedOrigin) { const urlObject = new URL(url); const originPattern = new RegExp(expectedOrigin.toLowerCase() + "$"); const isValid = urlObject.origin.toLowerCase().match(originPattern); if (!isValid) { throw new Error("Invalid URL"); } return `${urlObject.origin}${urlObject.pathname}`; } console.log(validateUrl("https://facebook.com/hi-there", "facebook.com")); //https://facebook.com/hi-there console.log(validateUrl("https://handbook.com/hi-there", "facebook.com")); // throws error Components of a URL. Source - MDN ...

August 18, 2023 · 2 min · Orim Dominic Adah
Dependency Injection in JavaScript blog header image

Dependency Injection in JavaScript

In building a house, after the architecture of the house has been drawn and accepted, everything built must fit the drawn architecture. A major change along the way may result in a complete tear-down and rebuild from scratch process. Once the building has been built with cement, it cannot be changed to wood. Building physical structures is a rigid process. Building software is different. Software, unlike physical structures, is expected to be flexible. If the requirements for a software being built changes along the way, it is expected that the changes are implemented without the need to tear it all down. New features, bug fixes, a change in the design system or database, all modify software without the need to tear it all down. ...

February 22, 2023 · 5 min · Orim Dominic Adah
setTimeout JavaScript

How does setTimeout Work with the JavaScript Engine?

If one wants a function to be executed just after a period of time T in JavaScript, what do they do? They dump it in a setTimeout and surely, just immediately after T has elapsed, the function will be run. Right? Can you predict what happens when the code below is run? You should try it out in a Node.js REPL or the console. const extractTime = (date) => date.toTimeString().split(" ")[0]; var start = new Date(); console.log("starting at", extractTime(new Date())); // #1 setTimeout(() => { const time = extractTime(new Date()); console.log(`From setTimeout. Logged at ${time}`); // #2 }, 1000); while (new Date() - start < 3000) { // empty while loop } // #3 What I ordered vs What I got For anyone that has not worked with code like this before, the expectation would be that #2 will be logged about 1 second after #1, but it was not. It was logged about 3 seconds after #1. Why? ...

January 4, 2023 · 3 min · Orim Dominic Adah
Callback Function JavaScript

What is a Callback Function in JavaScript

You know how you ask someone to call you back in a phone conversation? That’s what callback functions are. They are functions to be called later; after something has happened. In order to grasp this in practice, one needs to understand two things that there is a difference between a function name fn and a function call fn(), that functions can be passed into functions as arguments, the same way that numbers, strings and arrays are passed into functions as arguments The difference between a function name and a function call Let’s consider the code snippet below ...

August 7, 2022 · 5 min · Orim Dominic Adah
Asynchronous function javascript

What Is an Asynchronous Function in JavaScript?

When you look at any of the hands of a ticking clock, you will find out that it moves sequentially. It points at 1 before it points at 2, and then it points at 3 and it continues. It does not point at 4 and then 1 and then 9, in a random manner. It follows a sequence. Imagine that the hand is the JavaScript runtime and each number is a function. The clock’s hand, which represents the JavaScript runtime, determines what function is executed by pointing at the number. When it points at 1, function1 runs, and when it points at 2, function2 runs. ...

March 25, 2022 · 4 min · Orim Dominic Adah
Implement JavaScript's setInterval using setTimeout

Implement JavaScript's setInterval using setTimeout

I was in a job interview pair programming session where I was asked to implement JavaScript’s setInterval method without using setInterval itself. I did poorly at that interview for many reasons, including not knowing how to implement this. The Question Implement the setInterval function in such a way that executing new SetInterval.prototype.start will start the function passed to it and run the function at intervals interval milliseconds until the SetInterval.prototype.clear method is called. ...

June 23, 2021 · 3 min · Orim Dominic Adah