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
Javascript Articles
Page 125 of 534
Compute cartesian product of elements in an array in JavaScript
The Cartesian product of two sets (arrays) A and B, denoted A × B, is the set (array) of all ordered pairs (a, b) where a is in A and b is in B. In simpler terms, a cartesian product of two arrays is a permutation of all possible arrays of two elements whose first element belongs to the first array and the second element belongs to the second array. Example of Cartesian Product If the two arrays are: const arr1 = [1, 2, 3]; const arr2 = [4, 5]; Then their cartesian ...
Read MoreGroup by JavaScript Array Object
Suppose we have an array of arrays that contains the marks of some students in some subjects like this − const arr = [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ]; We are required to write a JavaScript function that takes in one such array and returns an object of objects. The return object should contain an object for each unique subject, and that object should contain information like the number of appearances ...
Read MoreHow to count the occurrence of a specific string in a string in JavaScript
In JavaScript, counting occurrences of a substring within a string is a common task. There are several approaches to accomplish this, from simple loops to built-in string methods. For example, counting how many times "is" appears in "this is a string" should return 2. count('this is a string', 'is') should return 2; Method 1: Using indexOf() with Loop The most reliable approach uses indexOf() to find each occurrence and increment a counter: const str1 = 'this is a string'; const str2 = 'is'; function countOccurrences(mainStr, subStr) { ...
Read MoreIterating through an array, adding occurrences of a true in JavaScript
Suppose we have an array of true/false values represented by 't'/'f' which we retrieved from some database like this − const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't']; console.log(arr); [ 'f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't' ] We need to count consecutive occurrences of 't' that are sandwiched between two 'f's and return an array of those counts. Array: ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', ...
Read MoreFind specific key value in array of objects using JavaScript
When working with JavaScript objects containing arrays of nested objects, you often need to find which parent key contains an object with specific property values. This is common when dealing with product catalogs, user groups, or categorized data. Example Data Structure Consider this product catalog object where each category contains an array of products: const obj = { "LAPTOP": [{ "productId": "123" }], "DESKTOP": [{ "productId": "456" ...
Read MoreSum of array object property values in new array of objects in JavaScript
When working with arrays of objects, we often need to group objects by a common property and sum up their numeric values. This is particularly useful when dealing with student records, sales data, or any scenario where duplicate categories need to be consolidated. Suppose we have an array of objects that contains data about students and their marks: const arr = [ { subject: 'Maths', marks: '40', noOfStudents: '5' }, { subject: 'Science', marks: '50', noOfStudents: '16' }, { subject: 'History', marks: '35', noOfStudents: '23' }, { subject: 'Science', ...
Read MoreCalculating Josephus Permutations efficiently in JavaScript
This problem takes its name from arguably the most important event in the life of the ancient historian Josephus. According to his tale, he and his 40 soldiers were trapped in a cave by the Romans during a siege. Refusing to surrender to the enemy, they instead opted for mass suicide, with a twist — they formed a circle and proceeded to kill one man every three, until one last man was left (and that it was supposed to kill himself to end the act). Josephus and another man were the last two and, as we now know ...
Read MoreHow to split comma and semicolon separated string into a two-dimensional array in JavaScript ?
Let's say we have a variable "users" that contains the following string of text where each user is separated by a semicolon and each attribute of each users is separated by a comma − const users = 'Bob, 1234, Bob@example.com;Mark, 5678, Mark@example.com'; We are required to write a JavaScript function that takes in one such string and splits this into a multidimensional array that looks like this − const arr = [ ['Bob', 1234, 'Bob@example.com'], ['Mark', 5678, 'Mark@example.com'] ]; Method 1: Using split() and for Loop ...
Read MoreFinding the least common multiple of a range of numbers in JavaScript?
We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range. The function should then calculate the least common multiple of all the numbers within that range and return the final result. Understanding LCM and GCD To find the LCM of multiple numbers, we first need helper functions for GCD (Greatest Common Divisor) and LCM of two numbers: GCD: The largest number that divides both numbers evenly LCM: The smallest positive number that is divisible by both numbers Formula: LCM(a, b) = (a × b) / ...
Read MoreHow to find a value is present in binary tree or not in JavaScript ?
We are required to write a JavaScript function on the prototype object of a BinarySearchTree data type that takes in a value and finds whether or not that value is contained in the BST. Binary Search Tree Structure A Binary Search Tree (BST) is a tree data structure where each node has at most two children. The left child contains values smaller than the parent, and the right child contains values greater than the parent. 50 30 ...
Read More