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

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

213 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
Updated on 15-Mar-2026 23:19:00

720 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
Updated on 15-Mar-2026 23:19:00

772 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
Updated on 15-Mar-2026 23:19:00

275 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

How to write an inline IF statement in JavaScript?

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

8K+ Views

Conditional statements are the most important and basic concept of any programming language. The if-else statement allows us to execute any code block conditionally. We can define the condition for the if statement in braces, and if the condition becomes true, it executes the code of the if block; Otherwise, it executes the code of the else block. Here, we have demonstrated how an if-else statement works in JavaScript. if (condition) { // code to execute when the condition becomes true } else { ... Read More

Remove smallest number in Array JavaScript

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

722 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
Updated on 15-Mar-2026 23:19:00

164 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
Updated on 15-Mar-2026 23:19:00

159 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

Combining two arrays in JavaScript

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

296 Views

In JavaScript, combining two arrays means pairing corresponding elements from each array to create a new array of subarrays. This is commonly known as "zipping" arrays together. For example, if we have two arrays of the same length, we can combine their elements at matching indices to form pairs. If the two arrays are: const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3]; Then the expected output should be: [ ['a', 1], ['b', 2], ['c', 3] ] ... Read More

Finding the sum of two numbers without using '+', '-', '/', '*' in JavaScript

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

1K+ Views

We are required to write a JavaScript function add() that takes in two numbers m and n. The function should, without using the four basic arithmetic operations add the two numbers taken as input and return the sum. The Challenge Without using +, -, *, or /, we need to implement addition using bitwise operations. This approach mimics how computers perform addition at the hardware level using binary logic. Example The code for this will be − const m = 67, n = 33; const add = (x, y) => { while(y !== 0){ let carry = x & y; x = x ^ y; y = carry

Advertisements