Articles on Trending Technologies

Technical articles with clear explanations and examples

Hyphen string to camelCase string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 245 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
AmitDiwan
Updated on 15-Mar-2026 586 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
AmitDiwan
Updated on 15-Mar-2026 839 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
AmitDiwan
Updated on 15-Mar-2026 256 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
AmitDiwan
Updated on 15-Mar-2026 235 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
AmitDiwan
Updated on 15-Mar-2026 222 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
Shubham Vora
Updated on 15-Mar-2026 769 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
AmitDiwan
Updated on 15-Mar-2026 526 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
AmitDiwan
Updated on 15-Mar-2026 210 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

Finding sum of all unique elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

In JavaScript, you can sum duplicate elements by counting their occurrences and multiplying each unique value by its count. This technique is useful for consolidating arrays with repeated values. For example, if the input array is: const input = [1, 3, 1, 3, 5, 7, 5, 4]; The output should be: [2, 6, 10, 7, 4] This means: 1 appears 2 times (1×2=2), 3 appears 2 times (3×2=6), 5 appears 2 times (5×2=10), while 7 and 4 appear once each. Using Map to Count and Sum The most efficient ...

Read More
Showing 16251–16260 of 61,297 articles
Advertisements