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 388 of 840
divisibleBy() function over array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers and a single number as two arguments. Our function should filter the array to contain only those numbers that are divisible by the number provided as second argument and return the filtered array. Example Following is the code − const arr = [56, 33, 2, 4, 9, 78, 12, 18]; const num = 3; const divisibleBy = (arr = [], num = 1) => { const canDivide = (a, b) => a % ...
Read MoreFinding 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 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 MoreRemove/ filter duplicate records from array - JavaScript?
JavaScript arrays often contain duplicate records that need to be removed. There are several effective methods to filter duplicates, depending on whether you're working with primitive values or objects. Sample Data with Duplicates Let's start with an array of nationality objects containing duplicate values: var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ]; console.log("Original array:", objectOfNationality); ...
Read MoreFinding the majority element of an array JavaScript
We are given an array of size n, and we are required to find the majority element. The majority element is the element that appears more than [ n/2 ] times. Example const arr = [2, 4, 2, 2, 2, 4, 6, 2, 5, 2]; const majorityElement = (arr = []) => { const threshold = Math.floor(arr.length / 2); const map = {}; for (let i = 0; i < arr.length; i++) { const value = arr[i]; ...
Read More