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 284 of 840
Sorting a JSON object in JavaScript
In JavaScript, objects themselves cannot be sorted since their properties don't have a guaranteed order. However, we can extract the values from a JSON object and sort them into an array. Suppose we have an object like this: const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; We need to write a JavaScript function that takes this object and returns a sorted array of its values: const arr = [11, 23, 56, ...
Read MoreThe algorithm problem - Backtracing pattern in JavaScript
Backtracking is a powerful algorithmic pattern for solving constraint satisfaction problems. In this grid path problem, we need to find all possible paths from start to end that visit every walkable square exactly once. Problem Definition Given a 2D grid with different square types, find the number of unique paths from start to end: 1 represents the starting square (exactly one) 2 represents the ending square (exactly one) 0 represents empty squares we can walk over -1 represents obstacles we cannot walk over ...
Read MoreConvert JSON array into normal json in JavaScript
Sometimes you receive data in a complex JSON array format with key-value pairs, but need to convert it to a simple flat object structure for easier access and manipulation. Problem Structure Consider this complex JSON array with nested key-value objects: const arr = [{ "key": "name", "value": "john" }, { "key": "number", "value": "1234" }, { "key": "price", "value": ...
Read MoreHow to take screenshot of a div with JavaScript
Taking a screenshot of a div element in JavaScript requires converting HTML elements to an image format. Since JavaScript cannot directly capture DOM elements as images, we use third-party libraries like html2canvas to achieve this functionality. The html2canvas library renders DOM elements onto a canvas element, which can then be converted to an image or downloaded. This approach works by parsing the CSS styles and creating a visual representation of the element. Installation and Setup To use html2canvas, include it via CDN or install it through npm: // Via CDN (add to HTML head) ...
Read MoreChecking for convex polygon in JavaScript
A convex polygon is a polygon where all interior angles are less than 180°. In a convex polygon, all vertices point "outward" and no line segment between any two points inside the polygon extends outside the polygon. Problem Statement We need to write a JavaScript function that takes an array of coordinates representing vertices of a polygon. Each coordinate is an array containing two numbers [x, y] representing a point on a 2D plane. The function should return true if the polygon is convex, false otherwise. For example, if the input is: const arr = ...
Read MoreGenerate all combinations of supplied words in JavaScript
In JavaScript, there are scenarios where you may need to generate every possible combination of a string's characters. This can be especially useful in areas like cryptography, analyzing subsets of data, or solving problems involving permutations. In this article, we'll learn to implement a JavaScript function to achieve this task. Generate all combinations of supplied words Following are the different approaches for generating all combinations of supplied words − Recursive Approach Iterative Approach Using Bitmasking Using Recursive Approach The recursive approach builds combinations by including ...
Read MoreGrouping an Array and Counting items creating new array based on Groups in JavaScript
When working with arrays of objects in JavaScript, you often need to group data by specific properties and count unique items. This article demonstrates how to group an array by region and count unique users per region. Problem Statement Suppose we have an array of objects representing user data across different regions: const arr = [ { region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, ...
Read MoreDeriving Random10() function from Random7() in JavaScript
Suppose we have a random7() function that generates random numbers from 1 to 7. We need to create a random10() function that generates random numbers from 1 to 10, using only the random7() function. Problem const random7 = () => Math.ceil(Math.random() * 7); This function yields a random number between 1 and 7 (inclusive) every time we call it. We need to write a random10() function that returns random numbers between 1 and 10 (inclusive) using only this random7() function. Using Rejection Sampling (Optimal Solution) The most efficient approach uses rejection sampling. We ...
Read MoreCompare the Triplets - JavaScript?
The "Compare the Triplets" problem is a popular algorithmic challenge where you compare two arrays of three integers each and count how many comparisons each array wins. Alice and Bob each have three scores, and we need to determine who wins more individual comparisons. Problem Statement Given two triplets (arrays of 3 elements each), compare corresponding elements and award points: 1 point to Alice if her element is greater, 1 point to Bob if his element is greater, 0 points for ties. Return an array with [Alice's score, Bob's score]. Example Input Alice: [5, 6, ...
Read MoreAdd matching object values in JavaScript
In JavaScript, you can add matching object values by iterating through an array of objects and accumulating values for common keys. This is useful for aggregating data from multiple objects. Consider an array of objects like this: const arr = [{a: 2, b: 5, c: 6}, {a: 3, b: 4, d: 1}, {a: 1, d: 2}]; console.log("Input array:", arr); Input array: [ { a: 2, b: 5, c: 6 }, { a: 3, b: 4, d: 1 }, { a: 1, d: 2 } ] Each object has unique properties within itself, ...
Read More