Constructing an object from repetitive numeral string in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

181 Views

Suppose, we have a string with digits like this − const str = '11222233344444445666'; We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string. So, for this string, the output should be − const output = { "1": 2, "2": 4, "3": 3, "4": 7, "5": 1, "6": 3 }; Using a for Loop The most straightforward approach is to iterate ... Read More

Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

3K+ Views

Given an array of five positive integers, we are required to find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. The array is not sorted all the times. For example: const arr = [1, 3, 5, 7, 9] The minimum sum is: 1 + 3 + 5 + 7 = 16 and the maximum sum is: 3 + 5 + 7 ... Read More

Build tree array from JSON in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

3K+ Views

Building a tree structure from a flat array is a common task in JavaScript. When you have hierarchical data represented as a flat array with codes indicating parent-child relationships, you can transform it into a nested tree structure. The Problem Suppose we have the following flat array where the code property indicates hierarchy through dot notation: const arr = [{ "code": "2", "name": "PENDING" }, { "code": "2.2", "name": "PENDING CHILDREN" }, { "code": "2.2.01.01", ... Read More

Flattening a JSON object in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

14K+ Views

Flattening a JSON object means converting a nested object structure into a single-level object where nested keys are represented using dot notation. This technique is useful for data processing, form handling, and API transformations. Suppose we have the following JSON object that may contain nesting up to any level: const obj = { "one": 1, "two": { "three": 3 }, "four": { "five": 5, "six": { "seven": 7 ... Read More

Number of carries required while adding two numbers in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

425 Views

When adding two numbers on paper, we sometimes need to "carry" a digit to the next column when the sum of digits exceeds 9. This article shows how to count the total number of carries required during addition. Problem We need to write a JavaScript function that takes two numbers and counts how many carries are needed when adding them digit by digit, just like manual addition on paper. For example, when adding 179 and 284: 9 + 4 = 13 (carry 1) 7 + 8 + 1 = 16 (carry 1) 1 + 2 + ... Read More

How to create a canvas with Image using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

925 Views

In this tutorial, we are going to learn how to create a canvas with Image using FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. Syntax new fabric.Image(element, options, callback) Parameters element − This parameter accepts HTMLImageElement, HTMLCanvasElement, HTMLVideoElement or String which denotes the image element. The String should be a URL and would be loaded as an image. ... Read More

How to replace before first forward slash - JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:19:00

882 Views

Let's say the following is our string with forward slash — var queryStringValue = "welcome/name/john/age/32" To replace before first forward slash, use replace() along with regular expressions. Syntax string.replace(/^[^/]+/, "replacement") Example Following is the code — var regularExpression = /^[^/]+/ var queryStringValue = "welcome/name/john/age/32" var replacedValue = queryStringValue.replace(regularExpression, 'index'); console.log("Original value=" + queryStringValue); console.log("After replacing the value=" + replacedValue); Original value=welcome/name/john/age/32 After replacing the value=index/name/john/age/32 How the Regular Expression Works The regular expression /^[^/]+/ breaks down as follows: ^ ... Read More

Finding the first unique element in a sorted array in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

636 Views

Suppose we have a sorted array of literals like this: const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false. For this array, the output should be 6. Using Index Comparison Method Since the array is sorted, we can check if an element is unique by comparing it with ... Read More

Group all the objects together having the same value for the '_id' key in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

418 Views

Suppose we have an array of objects like this − const arr = [ {_id : "1", S : "2"}, {_id : "1", M : "4"}, {_id : "2", M : "1"}, {_id : "" , M : "1"}, {_id : "3", S : "3"} ]; We are required to write a JavaScript function that takes in one such array and groups all the objects together that have the same value for the '_id' key. Therefore, the final output should ... Read More

Nested collection filter with JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

1K+ Views

Filtering nested collections in JavaScript requires searching through objects that contain arrays of other objects. Let's explore how to filter an array based on properties within nested objects. The Problem Suppose we have an array of nested objects where each object contains a legs array with carrier information: const arr = [{ id: 1, legs:[{ carrierName:'Pegasus' }] }, { id: 2, legs:[{ carrierName: 'SunExpress' }, ... Read More

Advertisements