AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 461 of 840

Finding shared element between two strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 634 Views

We are required to write a JavaScript function that takes in two strings that may / may not contain some common elements. The function should return an empty string if no common element exists otherwise a string containing all common elements between two strings. Following are our two strings − const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string'; Example Following is the code − const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string'; const ...

Read More

Return 5 random numbers in range, first number cannot be zero - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

We are required to write a JavaScript function that generates an array of exactly five unique random numbers. The condition is that all the numbers have to be in the range [0, 9] and the first number cannot be 0. Example Following is the code − const fiveRandoms = () => { const arr = [] while (arr.length < 5) { const random = Math.floor(Math.random() * 10); if (arr.indexOf(random) > -1){ ...

Read More

How to convert Array to Set in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 914 Views

This tutorial will teach you how to eliminate duplicate values from an array by converting it to a Set in JavaScript. Sets are collections that store only unique values. When you need to remove duplicates from an array or leverage the unique properties of Sets, converting an array to a Set is the most efficient approach. Using the Set Constructor (Recommended) The Set constructor accepts any iterable object, including arrays, and automatically removes duplicate values. Convert Array to Set - Using Set Constructor ...

Read More

Length of a JavaScript associative array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 718 Views

In JavaScript, arrays don't truly support associative arrays (key-value pairs with string keys). When you assign string keys to an array, they become object properties, not array elements, so the length property returns 0. To get the count of properties, use Object.keys(). The Problem with Array.length When you add string keys to an array, they don't count as array elements: var details = new Array(); details["Name"] = "John"; details["Age"] = 21; details["CountryName"] = "US"; details["SubjectName"] = "JavaScript"; console.log("Array length:", details.length); // 0, not 4! console.log("Type:", typeof details); Array length: 0 Type: ...

Read More

How can we make an Array of Objects from n properties of n arrays in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 214 Views

When working with multiple arrays in JavaScript, you often need to combine them into an array of objects. This is useful for creating structured data from separate arrays of related information. Suppose we have two arrays of literals like these: const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false]; We need to create a JavaScript function that combines these arrays into a new array of objects like this: [ {opt: 'A', val: true}, {opt: 'B', val: false}, {opt: 'C', val: false}, ...

Read More

Possible to split a string with separator after every word in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 336 Views

To split a string with separator after every word, you can use the split() method combined with filter() to remove empty elements that may result from consecutive separators. Syntax let result = string.split('separator').filter(value => value); Basic Example Let's start with a string that has separators between words: let sentence = "-My-Name-is-John-Smith-I-live-in-US"; console.log("Original string:", sentence); let result = sentence.split('-').filter(value => value); console.log("After split():"); console.log(result); Original string: -My-Name-is-John-Smith-I-live-in-US After split(): [ 'My', 'Name', 'is', 'John', 'Smith', 'I', 'live', ...

Read More

Finding letter distance in strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 779 Views

We are required to write a JavaScript function that takes in a string as first argument and two single element strings. The function should return the distance between those single letter strings in the string taken as first argument. For example − If the three strings are − const str = 'Disaster management'; const a = 'i', b = 't'; Then the output should be 4 because the distance between 'i' and 't' is 4 Understanding Letter Distance Letter distance is the absolute difference between the index positions of two characters in ...

Read More

Default exports vs Named exports in JavaScript

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

In this article, we will learn the difference between default exports and named exports in JavaScript, and how we can use them to effectively organize our code structure. In JavaScript, we can use default exports and named exports to have separate files or modules for separate pieces of code. This helps in enhancing code readability and tree shaking to a great extent. Default exports Named exports A ...

Read More

What is the simplest solution to flat a JavaScript array of objects into an object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

Flattening an array of objects into a single object combines all key-value pairs from the array elements. The simplest solution uses the reduce() method with the spread operator. Problem Example Consider an array of objects where each object contains different properties: const studentDetails = [ {Name: "Chris"}, {Age: 22} ]; console.log("Original array:", studentDetails); Original array: [ { Name: 'Chris' }, { Age: 22 } ] Using reduce() with Spread Operator The reduce() method iterates through the array and combines all objects into ...

Read More

How to insert an element into all positions in an array using recursion - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 569 Views

We are required to declare a function, let's say insertAllPositions, which takes two arguments — an element, x, and an array, arr. Functions must return an array of arrays, with each array corresponding to arr with x inserted in a possible position. That is, if arr is the length N, then the result is an array with N + 1 arrays — For example, the result of insertAllPositions(10, [1, 2, 3]) should be — const output = [ [10, 1, 2, 3], [1, 10, 2, 3], ...

Read More
Showing 4601–4610 of 8,392 articles
« Prev 1 459 460 461 462 463 840 Next »
Advertisements