Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 461 of 840
Finding shared element between two strings - JavaScript
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 MoreReturn 5 random numbers in range, first number cannot be zero - JavaScript
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 MoreHow to convert Array to Set in JavaScript?
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 MoreLength of a JavaScript associative array?
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 MoreHow can we make an Array of Objects from n properties of n arrays in JavaScript?
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 MorePossible to split a string with separator after every word in JavaScript
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 MoreFinding letter distance in strings - JavaScript
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 MoreDefault exports vs Named exports in JavaScript
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 MoreWhat is the simplest solution to flat a JavaScript array of objects into an object?
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 MoreHow to insert an element into all positions in an array using recursion - JavaScript?
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