Web Development Articles

Page 315 of 801

Delete elements in first string which are not in second string in JavaScript

Sakshi Jain
Sakshi Jain
Updated on 15-Mar-2026 230 Views

The problem statement asks us to delete elements from the first string that are not present in the second string. In other words, we need to keep only the characters from the first string that also exist in the second string, while preserving their original order. This problem can be viewed as finding the intersection of characters between two strings and returning a filtered version of the first string containing only common characters. What is a Map in JavaScript? We'll use a Map data structure to efficiently solve this problem. A Map is a collection of key-value ...

Read More

Sorting an array objects by property having null value in JavaScript

Sakshi Jain
Sakshi Jain
Updated on 15-Mar-2026 3K+ Views

When sorting arrays of objects in JavaScript, null values can disrupt the natural sorting order. This article shows how to sort objects by a property while ensuring null values appear at the end of the sorted array. Understanding the Problem When sorting objects by a numeric property, null values need special handling because they don't follow normal comparison rules. Our goal is to sort by the property value while pushing null values to the end. The Solution Approach We'll use a custom comparator function with JavaScript's sort() method. The key insight is to treat null values ...

Read More

Calculate the difference between the first and second element of each subarray separately and return the sum of their differences in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 400 Views

In JavaScript, we often need to process arrays of subarrays and perform calculations on their elements. This article demonstrates how to calculate the difference between the first and second element of each subarray and return the sum of all differences. What is an Array of Subarrays? An array of subarrays (or nested array) is an array that contains other arrays as its elements. Each inner array is called a subarray. const array = [[1, 2], [4, 5], [7, 8]]; console.log(array[0]); // First subarray console.log(array[1][0]); // First element of second subarray ...

Read More

Calculating the average for each subarray separately and then return the sum of all the averages in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 286 Views

In JavaScript, when working with arrays of subarrays, we often need to calculate the average for each subarray separately and then return the sum of all these averages. This can be efficiently accomplished using JavaScript's built-in array methods like reduce(). What is the reduce() Method in JavaScript? The reduce() method in JavaScript reduces an array to a single value by iterating over each element and applying a callback function that accumulates a value based on each iteration. It takes two main arguments: an accumulator (the accumulated value from previous iterations) and the current value being processed. ...

Read More

Check if items in an array are consecutive but WITHOUT SORTING in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 989 Views

In JavaScript, checking if array items are consecutive without sorting requires verifying that elements form an unbroken sequence. We can solve this using mathematical properties and built-in array methods. What are Consecutive Items in an Array? Consecutive elements form an unbroken sequence where each number differs by exactly 1 from the next. For example, [1, 2, 3, 4, 5] contains consecutive items in ascending order, while [5, 4, 3, 2, 1] is consecutive in descending order. Example Input/Output Input: [11, 12, 13] Output: true Input: [21, 11, 10] Output: false Method 1: ...

Read More

Convert number to characters in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 7K+ Views

In JavaScript, converting numbers to characters is commonly done using ASCII/Unicode values. JavaScript provides the built-in String.fromCharCode() method to convert numeric values to their corresponding characters. The String.fromCharCode() Method The String.fromCharCode() method converts Unicode values to characters. Each number represents a specific character in the ASCII/Unicode table. Syntax String.fromCharCode(num1, num2, ..., numN) Example: Single Number to Character const n = 65; const c = String.fromCharCode(n); console.log(c); console.log("Number 72 becomes:", String.fromCharCode(72)); console.log("Number 101 becomes:", String.fromCharCode(101)); A Number 72 becomes: H Number 101 becomes: e In this ...

Read More

Add all records from one array to each record from a different array in JavaScript

Sakshi Jain
Sakshi Jain
Updated on 15-Mar-2026 601 Views

In JavaScript, you may need to combine elements from two arrays where each record from one array pairs with every record from another array. This creates a Cartesian product - every possible combination of elements from both arrays. What is a Cartesian Product? A Cartesian product is a mathematical concept where given two sets A and B, A × B represents every possible combination of elements from both sets. In JavaScript, this translates to pairing each element from the first array with every element from the second array. For example, given two arrays: const array1 ...

Read More

Can the string be segmented in JavaScript

Sakshi Jain
Sakshi Jain
Updated on 15-Mar-2026 441 Views

The string segmentation problem determines if a given string can be broken down into words that exist in a provided dictionary. This is a classic dynamic programming problem with practical applications in natural language processing and text analysis. What is String Segmentation? String segmentation involves breaking a continuous string into meaningful words using a dictionary. For example, given the string "haveapplepie" and dictionary ["have", "apple", "pie"], we can segment it as "have" + "apple" + "pie". // Example visualization const dictionary = ["apple", "have", "pie"]; const input = "haveapplepie"; // Can be segmented as: "have" + ...

Read More

Case-sensitive sort in JavaScript

Sakshi Jain
Sakshi Jain
Updated on 15-Mar-2026 2K+ Views

JavaScript's default sort() method performs case-sensitive sorting but places uppercase letters before lowercase letters. This article demonstrates how to implement true case-sensitive sorting where special characters and numbers come first, followed by lowercase letters, then uppercase letters. JavaScript Case Sensitivity JavaScript is case-sensitive, meaning it treats uppercase and lowercase letters as completely different characters. const language = "JavaScript"; const Language = "React"; const x = 100; console.log(language); console.log(Language); console.log(x); JavaScript React 100 Default Sort Behavior JavaScript's sort() method converts elements to strings and sorts by UTF-16 character codes. By ...

Read More

Finding Common Item Between Arbitrary Number of Arrays in JavaScript

Sakshi Jain
Sakshi Jain
Updated on 15-Mar-2026 410 Views

Finding common elements among multiple arrays is a frequent requirement in JavaScript programming. This problem involves identifying elements that appear in every array within a collection of arrays. What are Arbitrary Number of Arrays An arbitrary number of arrays refers to any collection of multiple arrays (more than two) where we need to find intersection elements. These arrays can contain random elements and are typically organized as an array of arrays or nested data structure. // Example: Array of arrays const arrayCollection = [ [1, 2, 3, 4], [2, 3, 5, 6], ...

Read More
Showing 3141–3150 of 8,010 articles
« Prev 1 313 314 315 316 317 801 Next »
Advertisements