Recursive string parsing into object - JavaScript

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

607 Views

Recursive string parsing transforms an array of dot-separated strings into a nested JavaScript object structure. This technique is useful for converting flat configuration paths into hierarchical data structures. Problem Description Given an array of strings following the pattern x.y.x.y..., we need to create a nested object where the last segment becomes a value in an array, and preceding segments form the nested structure. For example, if the input array is: const arr = [ "country.UK.level.1", "country.UK.level.2", "country.US.level.1", "country.UK.level.3" ... Read More

Hyphen string to camelCase string in JavaScript

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

250 Views

Converting hyphen-separated strings to camelCase is a common requirement in JavaScript development. We need to transform strings like 'this-is-an-example' into 'thisIsAnExample'. Problem Statement Given a string with words separated by hyphens: const str = 'this-is-an-example'; We need to convert it into camelCase format: const output = 'thisIsAnExample'; Using split(), map(), and join() Method The most straightforward approach splits the string by hyphens, capitalizes each word except the first, and joins them back: const str = 'this-is-an-example'; const changeToCamel = str => { ... Read More

How to sort array by first item in subarray - JavaScript?

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

616 Views

In JavaScript, you can sort an array of subarrays based on the first element of each subarray using the sort() method with a custom comparison function. The Problem Consider an array where each element is itself an array, and you want to sort by the first item in each subarray: var studentDetails = [ [89, "John"], [78, "Mary"], [94, "Alice"], [47, "Bob"], [33, "Carol"] ]; Sorting in Descending Order To sort by the ... Read More

Return Vowels in a string in JavaScript

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

852 Views

We are required to write a JavaScript function that takes in a string that might contain some alphabets. The function should count and return the number of vowels that exists in the string. Syntax function countVowels(str) { // Convert to lowercase for case-insensitive comparison // Loop through each character // Check if character is a vowel (a, e, i, o, u) // Return the count } Example: Using for Loop Following is the code − const ... Read More

Finding the third maximum number within an array in JavaScript

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

269 Views

Finding the third maximum number in a JavaScript array requires handling duplicates and edge cases where fewer than three unique values exist. The task is to return the third largest unique number from an array. If fewer than three unique numbers exist, return the maximum number instead. Problem Example For the input array: [34, 67, 31, 87, 12, 30, 22] The unique numbers sorted in descending order are: [87, 67, 34, 31, 30, 22, 12] The third maximum is 34. Solution Approach The algorithm works by: ... Read More

Summing up to amount with fewest coins in JavaScript

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

244 Views

The coin change problem is a classic dynamic programming challenge where we need to find the minimum number of coins required to make a specific amount. If the amount cannot be achieved with the given coin denominations, we return -1. Problem Statement We need to write a JavaScript function that takes two parameters: arr: An array containing different coin denominations amount: The target amount we want to achieve The function should return the minimum number of coins needed to sum up to the target amount. Example Input and Output const arr ... Read More

Removing letters to make adjacent pairs different using JavaScript

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

231 Views

We are required to write a JavaScript function that takes in a string containing only 'A', 'B' and 'C'. Our function should find the minimum number of characters needed to be removed from the string so that no two adjacent characters are the same. Problem Statement Given a string with characters 'A', 'B', and 'C', we need to remove the minimum number of characters to ensure all adjacent pairs are different. For example, in "AAB", we need to remove one 'A' to get "AB". Algorithm Approach The strategy is to iterate through the string and whenever ... Read More

How to write a cell phone number in an international way using JavaScript?

Shubham Vora
Updated on 15-Mar-2026 23:19:00

800 Views

When you have visitors from worldwide on your websites, it is a good idea to show things like a cell phone number according to international standards. So they can easily understand. We can use the International Public Telecommunication Numbering Format to represent the cell phone number in an international way. Also, it is represented by the 'E-164' format. We have given the syntax below to follow to write a cell phone number in an international way. [+] [country code] [local phone number] Users can see that the international phone number contains '+' initially ... Read More

How to sort date array in JavaScript

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

546 Views

Sorting arrays of dates is a common task in JavaScript. This tutorial shows how to sort an array containing date strings in ascending chronological order. Suppose we have an array that contains some dates like this: const arr = [ [ '02/13/2015', 0.096 ], [ '11/15/2013', 0.189 ], [ '05/15/2014', 0.11 ], [ '12/13/2013', 0.1285 ], [ '01/15/2013', 0.12 ], [ '01/15/2014', 0.11 ], [ '02/14/2014', 0.11 ], ... Read More

Distance between 2 duplicate numbers in an array JavaScript

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

219 Views

We are required to write a JavaScript function that takes in an array of numbers that contains at least one duplicate pair of numbers. Our function should return the distance between all the duplicate pairs of numbers that exist in the array. The distance is calculated as the minimum difference between indices where duplicate numbers appear. Example Let's work with an array containing duplicates and find the minimum distance between each duplicate pair: const arr = [2, 3, 4, 2, 5, 4, 1, 3]; const findDistance = arr => { var ... Read More

Advertisements