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 by AmitDiwan
Page 325 of 840
Converting an unsigned 32 bit decimal to corresponding ipv4 address in JavaScript
Converting a 32-bit unsigned integer to an IPv4 address involves extracting the four bytes that represent each octet of the IP address. Each IPv4 address consists of four octets (0-255), which can be extracted using bitwise operations. Understanding the Problem Consider the IPv4 address: 128.32.10.1 When converted to binary, each octet becomes an 8-bit binary number: 10000000.00100000.00001010.00000001 Combining these four 8-bit segments creates a 32-bit binary number, which equals the decimal value: 2149583361 Our task is to reverse this process: convert the 32-bit unsigned integer back ...
Read MoreFinding peak of a centrally peaked array in JavaScript
A centrally peaked array is an array that increases to a peak element and then decreases. It has the following properties: Array length must be at least 3 There exists an index i where 0 < i < arr.length - 1 such that: arr[0] < arr[1] < ... < arr[i-1] < arr[i] (strictly increasing) arr[i] > arr[i+1] ...
Read MoreGroup a JavaScript Array
Grouping JavaScript arrays by a specific property is a common requirement when working with complex data structures. In this tutorial, we'll learn how to group array elements by the tableName property and restructure the data. Problem Statement Suppose we have a JavaScript array with objects containing table data: const data = [ { "dataId": "1", "tableName": "table1", "column": "firstHeader", "rows": ...
Read MoreCounting the number of IP addresses present between two IP addresses in JavaScript
We are required to write a JavaScript function that takes in two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one). This can be done by converting them to decimal and finding their absolute difference. How IP Address Conversion Works An IPv4 address consists of 4 octets (0-255). To convert to decimal, we use the formula: IP: a.b.c.d converts to: Decimal = a×256³ + b×256² + c×256¹ + d×256⁰ Example: 20.0.0.10 = 20×16777216 + 0×65536 + 0×256 + 10×1 ...
Read MoreRearranging cards into groups in JavaScript
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument. The numbers in the array are in the range [1, 13], limits inclusive, representing the 1-based index of playing cards. Our function should determine whether there exists a way to rearrange the cards into groups so that each group is size num, and consists of num consecutive cards. Problem Example For example, if the input to the function is: const arr = [1, 4, 3, 2]; ...
Read MoreSort array based on min and max date in JavaScript?
When working with arrays of date strings, you often need to find the oldest (minimum) and newest (maximum) dates. JavaScript provides several approaches to accomplish this task efficiently. const arr = [ "2017-01-22 00:21:17.0", "2017-01-27 11:30:23.0", "2017-01-24 15:53:21.0", "2017-01-27 11:34:18.0", "2017-01-26 16:55:48.0", "2017-01-22 11:57:12.0", "2017-01-27 11:35:43.0" ]; We need to write a JavaScript function that takes such an array, finds the oldest and newest date, and returns an object containing both dates. Using Array.reduce() Method The reduce() method provides an ...
Read MoreGenerating the sequence of first n look and say numbers in JavaScript
Problem In mathematics, the look-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, 312211, ... To generate a member of the sequence from the previous member, we read off the digits of the previous member, counting the number of digits in groups of the same digit. For instance, the next number to 1211 is: 111221 Because if we read the digits of 1211 aloud it will be: One one, one two, two one which gives us 111221 We are required to write a JavaScript ...
Read MoreMaximum length of mountain in an array using JavaScript
A mountain subsequence in JavaScript is a contiguous subarray that first increases then decreases, forming a mountain-like pattern. This article explains how to find the maximum length of such a subsequence. Mountain Subsequence Definition A subarray is considered a mountain if it meets these criteria: Length ≥ 3 elements Elements strictly increase to a peak, then strictly decrease Pattern: sub[0] < sub[1] < ... < sub[i] > sub[i+1] > ... > sub[n-1] Peak ...
Read MoreCalculate average from JSON data based on multiple filters JavaScript
When working with JSON data, you often need to filter, group, and calculate averages based on multiple criteria. This article demonstrates how to group objects by multiple fields and compute averages while handling edge cases like undefined values. Problem Statement Given an array of supplier objects, we need to: Group objects with the same "SupplierName" and "Category" Sum their points together (ignoring undefined values) Calculate the average points for each group Return the grouped results with totals and averages ...
Read MoreRectifying the time string in JavaScript
In JavaScript, we often need to rectify invalid time strings where the minutes or seconds exceed their maximum values (59). This function converts an invalid time format like "08:11:71" into a valid one by carrying over excess values to the next time unit. Problem We need to write a JavaScript function that takes in a time string in "HH:MM:SS" format. However, many time strings are broken, meaning the MM part may exceed 60, and SS part may exceed 60 as well. Our function should make required changes and return the rectified string. For instance: "08:11:71" ...
Read More