AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 312 of 840

JavaScript Bubble sort for objects in an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 614 Views

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list and swapping adjacent elements if they are in the wrong order. When applied to an array of objects, we can sort based on specific properties. The Shoe Class Let's start with a constructor class that creates Shoe objects: class Shoe { constructor(name, price, type) { this.name = name; this.price = price; this.type = ...

Read More

Number of digits that divide the complete number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should count and return the number of digits present in the number that completely divide the number. For example, if the input number is 148, the output should be 2 because 148 is exactly divisible by 1 and 4 but not 8. Example Input and Output Input: 148 Output: 2 This is because: 148 ÷ 1 = 148 (divisible) 148 ÷ 4 = 37 (divisible) ...

Read More

Turn each character into its ASCII character code and join them together to create a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 432 Views

Problem We need to write a JavaScript function that takes a string and converts each character to its ASCII code, joins them to create a number, replaces all instances of 7 with 1, and returns the difference between the original and modified numbers. Understanding the Process The algorithm involves several steps: Convert each character to its ASCII code using charCodeAt() Join all ASCII codes to form one large number Replace all occurrences of digit 7 with 1 Calculate the difference between original and modified numbers ...

Read More

Placing integers at correct index in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

We need to write a JavaScript function that takes a string containing only square brackets '[' and ']' and determines the minimum number of brackets to add to make it balanced. Problem Statement Given a string consisting of only '[' and ']' characters, find the minimum number of brackets that need to be added to make the string valid (properly balanced). A valid bracket string means every opening bracket '[' has a corresponding closing bracket ']' that comes after it. Example Input and Output Input: const str = '[]]'; Output: 1 ...

Read More

Fetching odd appearance number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

Given an array of integers, we need to find the element that appears an odd number of times. There will always be exactly one such element. We can solve this problem using different approaches. The sorting approach iterates through a sorted array to count occurrences, while XOR provides an elegant mathematical solution. Method 1: Using Sorting This approach sorts the array first, then iterates through it to count consecutive identical elements. const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOddSorting = ...

Read More

Converting a comma separated string to separate arrays within an object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 672 Views

When working with comma-separated strings containing hierarchical data, we often need to parse them into structured objects. This article shows how to convert a string like 'dress/cotton/black, dress/leather/red' into an organized object with arrays. The Problem Suppose we have a string like this: const str = 'dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james'; We need to create a JavaScript function that converts this into an object where each category becomes a key, and all related values are grouped into arrays: const output = { dress: ["cotton", "black", "leather", "red", "fabric"], ...

Read More

Longest string with two distinct characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 443 Views

We need to write a JavaScript function that finds the longest substring containing at most two distinct characters. The function takes a string as input and returns the length of the longest valid substring. For example, if the input string is 'kjeljsdl', the longest substring with at most 2 distinct characters is 'jljl' with length 4. Understanding the Problem A substring with at most two distinct characters means we can have: Only one character repeated: "aaa" Two different characters in any combination: "abab", "aabb" Sliding Window Approach The most efficient solution uses a ...

Read More

Count number of entries in an object having specific values in multiple keys JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 348 Views

In JavaScript, you often need to count objects in an array that match specific criteria across multiple properties. This is commonly done using the filter() method combined with conditional logic. Suppose we have an array of objects representing trade data: const arr = [ {"goods":"Wheat", "from":"GHANA", "to":"AUSTRALIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ]; We need to count objects where the "from" property equals "USA" and the "to" property equals "INDIA". Using filter() and length ...

Read More

Maximum subarray sum in circular array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 268 Views

We need to write a JavaScript function that finds the maximum possible sum of a subarray in a circular array. In a circular array, the last element connects to the first element, creating additional subarray possibilities. Problem Statement Given an array of integers, find the maximum sum of any non-empty subarray. The array is considered circular, meaning we can wrap around from the end to the beginning. Example: Input: [2, -2, 3, -1] Output: 4 Explanation: Maximum subarray is [3, -1, 2] (wrapping around) Algorithm Approach The solution uses two key insights: ...

Read More

How to sort a JavaScript object list based on a property when the property is not consistent

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 166 Views

When sorting JavaScript object arrays with inconsistent properties, you need a custom comparator function. This scenario commonly occurs when some objects have date values while others have null or undefined dates. The requirement is to display objects without dates at the top (sorted alphabetically by name), followed by objects with dates (sorted chronologically). Problem Overview Consider an array where some objects have date properties and others don't: const items = [ { name: "Charlie", date: "2024-03-15" }, { name: "Alice", date: null }, ...

Read More
Showing 3111–3120 of 8,392 articles
« Prev 1 310 311 312 313 314 840 Next »
Advertisements