AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 286 of 840

How to get only first word of object's value – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 476 Views

When working with objects that contain string values with multiple words, you often need to extract only the first word. The most effective approach is using the split() method with a space delimiter. Sample Data Let's work with an employee object containing names and technologies: const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java ...

Read More

Reverse alphabetically sorted strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse alphabetical order i.e., 'z' should come before 'y', 'b' before 'a', and so on. Example If the input string is: const str = "hello"; Then the output should be: const output = "ollhe"; Using Custom Comparator Function Here's how we can achieve reverse alphabetical sorting using a custom comparator: const string = 'hello'; const sorter = (a, b) => { const legend = ...

Read More

Finding two numbers that produce equal to the sum of rest in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 366 Views

We have a sequence of numbers from 1 to any arbitrary number. We need to find pairs of numbers (m and n) from this sequence such that the sum of all remaining numbers equals the product of these two numbers. Problem Statement Given a number num, find all pairs [m, n] where: sum(1 to num) - (m + n) = m * n For example, if num = 10: Sum of 1 to 10 = 55 For pair [6, 7]: 55 - (6 + 7) = 42 and 6 × 7 = 42 ...

Read More

Finding smallest sum after making transformations in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 Views

We need to write a JavaScript function that takes an array of positive integers and applies transformations until no more are possible. The transformation rule is: if arr[i] > arr[j], then arr[i] = arr[i] - arr[j]. After all transformations, we return the sum of the array. Problem The key insight is that this transformation process eventually reduces all numbers to their Greatest Common Divisor (GCD). When we repeatedly subtract smaller numbers from larger ones, we're essentially performing the Euclidean algorithm. if arr[i] > arr[j] then arr[i] = arr[i] - arr[j] How It Works ...

Read More

Constructing an object from repetitive numeral string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 176 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
AmitDiwan
Updated on 15-Mar-2026 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
AmitDiwan
Updated on 15-Mar-2026 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
AmitDiwan
Updated on 15-Mar-2026 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
AmitDiwan
Updated on 15-Mar-2026 413 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 replace before first forward slash - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 876 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
Showing 2851–2860 of 8,392 articles
« Prev 1 284 285 286 287 288 840 Next »
Advertisements