Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Finding minimum time difference in an array in JavaScript
We are required to write a JavaScript function that takes in an array of 24-hour clock time points in "Hour:Minutes" format. Our function should find the minimum minutes difference between any two time points in the array. For example, if the input to the function is: const arr = ["23:59", "00:00"]; Then the output should be 1 minute, because the minimum difference between these times is 1 minute (from 23:59 to 00:00). Understanding the Problem The key challenge is handling the circular nature of time. When comparing "23:59" and "00:00", we need to ...
Read MoreWhat are JavaScript Factory Functions?
A factory function is a function that creates and returns an object. Unlike constructor functions, factory functions don't require the new keyword and don't use this to reference object properties. Factory functions are a simple and flexible way to create objects in JavaScript. They can include properties, methods, and default values, making object creation more convenient and reusable. Key Features Factory functions offer several advantages: No need for the new keyword No this binding issues Can contain private variables through closures Return objects directly Allow for easy object customization Basic Syntax ...
Read MoreHow to check the OffscreenCanvas is supported by the Browser or not in JavaScript?
In HTML, Canvas is very important when we want to show animation or 3D objects on the webpage using only HTML and JavaScript. The OffscreenCanvas allows users to render animations and graphics off-screen. It means when we use regular canvas, it interacts with the user via the main thread of the web application, but OffscreenCanvas runs in a separate thread. This improves application performance by preventing graphics rendering from blocking the main UI thread. Before using OffscreenCanvas, we need to check if it's supported by the browser. Otherwise, we should fall back to regular canvas. Let's explore two ...
Read MoreHow to skip over an element in .map()?
In JavaScript, we sometimes need to skip array elements while using the map() method. For example, we might want to map values from one array to another after performing mathematical operations only on finite values, or transform strings that meet specific criteria. Here are three effective approaches to skip over array elements while using the map() method. Using if-else Statement In the array.map() method, we can use an if-else statement to conditionally process elements. If an element meets our condition, we return the transformed value; otherwise, we return null or undefined. Syntax array.map((element) => ...
Read MoreHow do I turn a string in dot notation into a nested object with a value – JavaScript?
Converting a dot-notation string into a nested object is a common task in JavaScript. This approach uses split() and map() to dynamically build the nested structure. Problem Setup Let's say we have a dot-notation string representing nested property keys: const keys = "details1.details2.details3.details4.details5"; const firstName = "David"; We want to create a nested object where the final property gets the value "David". Solution Using split() and map() The technique involves splitting the dot-notation string and using map() to build nested objects: const keys = "details1.details2.details3.details4.details5"; const firstName = "David"; ...
Read MoreFinding sum of a range in an array JavaScript
We are required to write an Array function (functions that lives on Array.prototype object). The function should take in a start index and an end index and it should sum all the elements from start index to end index in the array (including both start and end). Example const arr = [1, 2, 3, 4, 5, 6, 7]; const sumRange = function(start = 0, end = 1){ let sum = 0; if(start > end){ return sum; } for(let i = start; i
Read MoreSplit number into 4 random numbers in JavaScript
We need to write a JavaScript function that takes a total number and a maximum value, then generates four random numbers that sum to the total while ensuring none exceeds the maximum. The function should generate four random numbers where: All four numbers sum to the given total No number exceeds the specified maximum Repetition of numbers is allowed For example, if the total is 10 and maximum is 4, a valid output could be [3, 2, 3, 2]. Algorithm Overview The algorithm works by: Generating random values between 0 and 1 Scaling ...
Read MoreCounting How Many Numbers Are Smaller Than the Current Number in JavaScript
In JavaScript, we often need to count how many elements in an array are smaller than each current element. This creates a new array where each position contains the count of smaller elements from the original array. Problem Statement Given an array of numbers, create a new array where each element represents the count of numbers smaller than the corresponding element in the original array. For example, if the input array is: [2, 7, 3, 1, 56, 4, 7, 8] The output should be: [1, 4, 2, 0, 7, 3, 4, ...
Read MoreImplementing partial sum over an array using JavaScript
We need to write a JavaScript function that takes an array of numbers and returns a new array where each element represents the sum of all elements from that position to the end of the original array (including the current element). Problem Given an array of numbers, construct a new array where each corresponding element is the sum of all elements from that position to the right (including itself) in the input array. Example Input and Expected Output For array [5, 6, 1, 3, 8, 11], the partial sum array should be: Position 0: 5 ...
Read MoreCreating a chained operation class in JavaScript
This article demonstrates how to create a fluent interface class in JavaScript that allows chaining numeric values and mathematical operations. The Streak class enables natural language-like mathematical expressions. Problem We need to create a custom data type Streak in JavaScript that supports method chaining with values and operations alternately. The supported values are: → one, two, three, four, five, six, seven, eight, nine The supported operations are: → plus, minus For example, this expression: Streak.one.plus.five.minus.three; Should evaluate to: 3 How It ...
Read More