AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 367 of 840

Third smallest number in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 641 Views

Problem We are required to write a JavaScript function that takes in an array of numbers of length at least three. Our function should simply return the third smallest number from the array. Example Following is the code − const arr = [6, 7, 3, 8, 2, 9, 4, 5]; const thirdSmallest = () => { const copy = arr.slice(); for(let i = 0; i < 2; i++){ const minIndex = copy.indexOf(Math.min(...copy)); copy.splice(minIndex, ...

Read More

Changing second half of string number digits to zero using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 243 Views

Problem We are required to write a JavaScript function that takes in a string number as the only argument. Our function should return the input number with the second half of digits changed to 0. In cases where the number has an odd number of digits, the middle digit onwards should be changed to 0. For example: 938473 → 938000 How It Works The algorithm works by calculating the midpoint of the string. For even-length strings, exactly half the digits are preserved. For odd-length strings, the middle digit becomes part of ...

Read More

Convert number to a reversed array of digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

Given a non-negative integer, we need to write a function that returns an array containing the digits in reverse order. This is a common programming challenge that demonstrates string manipulation and array operations. Example 348597 => The correct solution should be [7, 9, 5, 8, 4, 3] Method 1: Using String Split and Reverse The most straightforward approach is to convert the number to a string, split it into individual characters, reverse the array, and convert back to numbers: const num = 348597; const reverseArrify = num => { ...

Read More

How to automate this object using JavaScript to set one key on each iteration as null?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 203 Views

When working with JavaScript objects, you might need to systematically set each property to null one at a time. This can be useful for testing, validation, or creating variations of an object. The most efficient approach is using Object.keys() with the spread operator. Understanding the Problem Given an object with multiple properties, we want to create new objects where exactly one property is set to null in each iteration, while keeping all other properties unchanged. Using Object.keys() with Spread Operator The Object.keys() method returns an array of property names, which we can iterate through. The spread ...

Read More

Finding nth digit of natural numbers sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 681 Views

In this problem, we need to find the nth digit in the infinite sequence formed by concatenating natural numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12... When we remove commas and spaces, this becomes: "123456789101112..." and we need to find the digit at a specific position. Understanding the Problem The natural number sequence when concatenated forms: Position: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Digit: 1 2 3 4 5 6 7 8 9 ...

Read More

Switching on and off bulb in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 758 Views

Consider this problem: There are n bulbs that are initially off. We first turn on all the bulbs. Then, we turn off every second bulb. On the third round, we toggle every third bulb (turning on if it's off or turning off if it's on). In general, for the ith round, we toggle every i bulb. For the nth round, we only toggle the last bulb. We need to find how many bulbs are on after n rounds. Problem Example For n = 5, here's what happens: Round Action State [1, 2, 3, ...

Read More

Common element with least index sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

We need to find common elements between two arrays that have the smallest sum of their indices from both arrays. When multiple elements tie for the smallest sum, we return all of them. Problem Understanding Given two arrays, for each common element, we calculate the sum of its index in the first array and its index in the second array. The element(s) with the minimum index sum are returned. For example, with arrays: const arr1 = ['a', 'b', 'c', 'd']; const arr2 = ['d', 'a', 'c']; Common elements and their index sums: ...

Read More

Remove smallest number in Array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 714 Views

In JavaScript, removing the smallest number from an array can be accomplished in several ways. This article demonstrates different approaches to find and remove the smallest element from an array in place. Method 1: Using reduce() and splice() This approach uses reduce() to find the index of the smallest element, then splice() to remove it: const arr = [2, 1, 3, 2, 4, 5, 1]; const removeSmallest = arr => { const smallestCreds = arr.reduce((acc, val, index) => { let { num, ind ...

Read More

Upper or lower elements count in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 158 Views

When working with arrays of numbers, you may need to count how many elements are above or below a specific threshold value. This is useful for data analysis, filtering, and statistical operations. Consider we have an array of numbers that looks like this: const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; We need to write a function that counts how many elements in the array are below and above a given number. For example, if the threshold number is 60: Elements below ...

Read More

How to decrease size of a string by using preceding numbers - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 150 Views

Let's say our original string is the following with repeated letters − var values = "DDAAVIDMMMILLERRRRR"; We want to remove the repeated letters and precede letters with numbers. For this, use replace() along with regular expression. Syntax string.replace(/(.)\1+/g, match => match.length + match[0]) How It Works The regular expression /(.)\1+/g matches any character followed by one or more repetitions of the same character. The replacement function returns the count plus the original character. Example Following is the code − var values = "DDAAVIDMMMILLERRRRR"; var precedingNumbersInString = ...

Read More
Showing 3661–3670 of 8,392 articles
« Prev 1 365 366 367 368 369 840 Next »
Advertisements