AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 296 of 840

Sorting numbers based on their digit sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 628 Views

We are required to write a JavaScript function that takes in an array of positive integers and sorts them based on their digit sums in descending order. Numbers with higher digit sums appear first. Problem Statement Given an array of positive integers, sort the array so that numbers with the highest digit sum come first, followed by numbers with lesser digit sums. For example, if the input array is: const arr = [5, 34, 1, 13, 76, 8, 78, 101, 57, 565]; The output should be: [565, 78, 76, 57, 8, ...

Read More

Number to alphabets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 916 Views

We are required to write a JavaScript function that takes in a string of any variable length that represents a number. Our function is supposed to convert the number string to the corresponding letter string. For example − If the number string is − const str = '78956'; Then the output should be − const output = 'ghief'; If the number string is − const str = '12345'; Then the output string should be − const output = 'lcde'; Notice how we ...

Read More

Modify an array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

When working with arrays in JavaScript, you might need to modify one array based on patterns or elements found in another array. This article demonstrates how to join consecutive elements from a reference array when they appear as combined phrases in a second array. Problem Statement Suppose we have a reference array of individual words: const reference = ["your", "majesty", "they", "are", "ready"]; And we want to modify it based on another array that contains some words joined together: const another = ["your", "they are"]; The goal is to create ...

Read More

Finding the length of second last word in a sentence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 530 Views

A sentence is just a string which contains strings (called words) joined by whitespaces. We are required to write a JavaScript function that takes in one such sentence string and count the number of characters in the second to last word of the string. If the string contains no more than 2 words, our function should return 0. For example − If the input string is − const str = 'this is an example string'; Then the output should be − const output = 7; because the number of characters ...

Read More

Finding life path number based on a date of birth in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 537 Views

A person's Life Path Number is calculated by adding each individual digit in that person's date of birth, then reducing it to a single digit number through repeated digit summation. Problem We need to write a JavaScript function that takes a date in "yyyy-mm-dd" format and returns the life path number for that date of birth. How It Works The calculation involves three steps: Sum all digits in the year, month, and day separately Reduce each sum to a single digit by adding digits repeatedly Add the three single digits and reduce to final ...

Read More

Group Similar Items in JSON in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Suppose, we have a JSON Array that contains data about some tickets like this: const arr = [ { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": ...

Read More

Number of letters in the counting JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 434 Views

We are required to write a JavaScript function that takes in a number, say n. The function should count the letters in the number names from 1 to n. For example − If n = 5; Then the numbers are one, two, three, four, five. And the total number of letters are 19, so the output should be 19. How It Works The algorithm uses arrays to store the letter count for each digit position and handles special cases like teens (11-19) and compound numbers (hundred, thousand). Example const sumUpto = (num = ...

Read More

Finding desired numbers in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 267 Views

We have an array of integers which is sorted in the increasing order. We are required to write a JavaScript function that takes in one such array as the first argument and a target sum number as the second argument. The function should find and return two such numbers from the array that when added gives the target sum. The condition for solving this problem is that we have to do this in linear time and using constant space. Two Pointer Approach The optimal solution uses the two-pointer technique. Since the array is sorted, we can place ...

Read More

Smallest number formed by shuffling one digit at most in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

We need to write a JavaScript function that takes a positive number and can perform at most one operation: remove a digit from any position and insert it at another position to create the smallest possible number. Problem Statement Given a positive number, we can choose any digit, remove it from its current position, and insert it at any other position (including the same position). The goal is to find the smallest number possible with this single operation. Algorithm Approach The strategy is to find the smallest digit in the number and move it to the ...

Read More

Limiting elements occurrences to n times in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 298 Views

Problem We are required to write a JavaScript function that takes in an array of integers, arr, that may contain duplicates as the first argument, and a number, num, as the second and final argument. The task of our function is to iterate through the array and check whether there exists some number that appears for more than n times in the array. If there exists any such element, we should delete its extra occurrences to limit its occurrence to at most num. For example, if the input to the function is: Input ...

Read More
Showing 2951–2960 of 8,392 articles
« Prev 1 294 295 296 297 298 840 Next »
Advertisements