Web Development Articles

Page 181 of 801

Merge two objects in JavaScript ignoring undefined values

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

When merging two objects in JavaScript, you may want to ignore undefined values to avoid overwriting valid data. The spread operator alone doesn't handle this logic, so we need a custom approach. The Problem Consider these two objects where some properties have undefined values: const A = { activity: 'purchased', count: undefined, time: '09:05:33' }; const B = { activity: 'purchased', count: '51', time: undefined }; // Simple spread operator overwrites values const simpleSpread = { ...A, ...B }; console.log("Simple spread:", simpleSpread); Simple spread: { activity: 'purchased', count: '51', time: undefined } ...

Read More

Checking if one string can be achieved from another with single tweak in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 151 Views

We are required to write a JavaScript function that takes in two strings of characters lets call them str1 and str2. The function should check whether we can form str2 from str1 by deleting exactly one character from str1. If we can do so the function should return true, false otherwise. For example − If the input strings are − const str1 = 'chemistty'; const str2 = 'chemisty'; Then the output should be − const output = true; Approach The solution involves checking if the length difference is ...

Read More

Convert array into array of subarrays - JavaScript

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

We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element. For example, if the input array is: const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be: const output = [[1, 2], [3, 4], [5, 6], [7]] Using Loop-based ...

Read More

Counting the number of palindromes that can be constructed from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 414 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument. The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count. Problem Example If the input string and the number is: const str = 'ij'; const num = 4; Then the output should be: 4 because those four possible palindrome ...

Read More

JavaScript Algorithm - Removing Negatives from the Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), we need to write a function that removes any negative values from the array using only the pop() method without creating temporary arrays. The challenge is to modify the original array in-place, maintaining only positive numbers while using just the pop() method for removal. Algorithm Approach The algorithm works in two phases: Remove all negative values from the end of the array For remaining negatives, replace them with the last positive element and pop Complete Implementation function ...

Read More

Counting How Many Numbers Are Smaller Than the Current Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

In JavaScript, we often need to count how many elements in an array are smaller than each current element. This creates a new array where each position contains the count of smaller elements from the original array. Problem Statement Given an array of numbers, create a new array where each element represents the count of numbers smaller than the corresponding element in the original array. For example, if the input array is: [2, 7, 3, 1, 56, 4, 7, 8] The output should be: [1, 4, 2, 0, 7, 3, 4, ...

Read More

How to join JavaScript array of string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

We need to write a JavaScript function that joins an array of strings, replaces all whitespaces with dashes "-", and returns the formatted string. For example: If the array is − const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; Then the output should be − const output = "QA-testing-promotion-Twitter-Facebook-Test"; Method 1: Using Loop and String Concatenation This approach joins the array elements and processes each character to replace spaces with dashes: const arr = ["QA testing promotion ", " Twitter ", "Facebook ", ...

Read More

Recursive string parsing into object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 600 Views

Recursive string parsing transforms an array of dot-separated strings into a nested JavaScript object structure. This technique is useful for converting flat configuration paths into hierarchical data structures. Problem Description Given an array of strings following the pattern x.y.x.y..., we need to create a nested object where the last segment becomes a value in an array, and preceding segments form the nested structure. For example, if the input array is: const arr = [ "country.UK.level.1", "country.UK.level.2", "country.US.level.1", "country.UK.level.3" ...

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

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
Showing 1801–1810 of 8,010 articles
« Prev 1 179 180 181 182 183 801 Next »
Advertisements