We know that there is no true random number. The best we get is pseudo-random number, which comes from a seed value. I was wondering if I could get a random number without using Math.random() function of JavaScript. So I developed an algorithm of my own, which is a few lines of code that works by keeping, updating, and shifting a seed array in the state.
[Read More]
Compare Two JavaScript Objects and Get the Updated Keys
Where a Value of the Key Has Changed
We have two similar JavaScript objects, and we are interested in finding which of the key(s), if any, have changed at the first level. A key in the object could hold any data type in its value, including array and object, and it could also be deeply nested. The change could mean anything, such as added, added to, removed, removed from, modified, or shuffled etc.
This kind of key identification might be required in cases where you need to compare the old and new state, such as previous and new filters, and figure out what exactly changed.
[Read More]JavaScript Token Bucket Algorithm
And its Possible Uses
Token Bucket is a rate-limiting algorithm in which there is a bucket that gets refilled at a particular rate with tokens. To perform an action, one or more tokens can be taken out or redeemed from this bucket. When there are no more tokens left, the action cannot be performed until the bucket is refilled and further tokens are available.
[Read More]
CPU Scheduling Algorithms - JavaScript
Simulation Code for Scheduler and Task
In this post, I am going to share a JavaScript simulation for some well-known CPU/operation system scheduling algorithms. The ES6 JavaScript code can be run in the Node JS environment.
[Read More]JavaScript Get Information of Week Days Between Two Dates
With and Without Moment JS
Requirement: Given a start and end date, we need to find out which days of the week exist between them i.e. Sun, Mon, Tue, Wed, Thu, Fri, Sat (in numbers: 0, 1, 2, 3, 4, 5, 6). We might also want to count them, or we may only be interested if weekends exist.
[Read More]
JavaScript Flatten Deeply Nested Array of Objects Into Single Level Array
Using plain JavaScript, and lodash's flatMapDeep method
Requirement: We have a deeply nested array of objects. We want to bring all the nested objects into the array at the root level.
Following is the example array familyTree that has multiple people in the root and many of them have children array containing further members:
[Read More]
JavaScript Find Path of Key in Deeply Nested Object or Array
Key Path Finder Using Depth First Search (DFS)
The following two codes go through the whole object, an array of objects, or a collection of both to get the path to a particular key. There are two versions: first gets the path to the key only, and second gets the path where a key has the given value.
[Read More]