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
Web Development Articles
Page 231 of 801
How to build a string with no repeating character n separate list of characters? in JavaScript
When working with multiple arrays of characters, we often need to create combinations where each string contains exactly one character from each array with no duplicate characters. This is useful for generating unique combinations while avoiding repeated letters that might exist across different arrays. Problem Overview Given multiple arrays of characters, we need to build all possible strings that: Contains exactly one letter from each array ...
Read MoreSplit number into 4 random numbers in JavaScript
We need to write a JavaScript function that takes a total number and a maximum value, then generates four random numbers that sum to the total while ensuring none exceeds the maximum. The function should generate four random numbers where: All four numbers sum to the given total No number exceeds the specified maximum Repetition of numbers is allowed For example, if the total is 10 and maximum is 4, a valid output could be [3, 2, 3, 2]. Algorithm Overview The algorithm works by: Generating random values between 0 and 1 Scaling ...
Read MoreHow to check if an array contains integer values in JavaScript ?
In JavaScript, checking if an array contains integer values requires understanding the difference between numbers and strings that look like numbers. This article explores different approaches to detect actual integer values in arrays. The Problem with String Numbers Arrays often contain string representations of numbers like "123" instead of actual numbers. We need to distinguish between these types. const mixedArray = ["123", 45, "hello", 67.5, "89"]; console.log(typeof "123"); // "string" console.log(typeof 45); // "number" string number Method 1: Using typeof and Number.isInteger() The most ...
Read MoreHow to convert nested array pairs to objects in an array in JavaScript ?
When working with complex data structures, you often need to convert nested array pairs into objects. This is common when processing form data or API responses that use key-value pair arrays. Problem Statement Suppose we have an array of arrays like this: const arr = [ [ ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'] ], ...
Read MoreSort an array of objects by multiple properties in JavaScript
Sorting an array of objects by multiple properties is a common requirement in JavaScript applications. This involves creating custom comparison logic that prioritizes different properties based on specific criteria. Suppose, we have an array of objects like this: const arr = [ { id: 1, score: 1, isCut: false, dnf: false }, { id: 2, score: 2, isCut: false, dnf: false }, { id: 3, score: 3, isCut: false, dnf: false }, { id: 4, score: 4, isCut: false, dnf: false ...
Read MoreHighest occurrence in an array or first selected in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. Our function should then return the highest occurrence of an array value, and if there's an equal occurrence, we should return the first selected value of the equal occurrences. Problem Statement Given an array of values, find the element that appears most frequently. If multiple elements have the same highest frequency, return the one that appears first in the array. const arr1 = ['25', '50', 'a', 'a', 'b', 'c']; // 'a' appears 2 times (most frequent), so return 'a' ...
Read MoreFind all substrings combinations within arrays in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should find all the substring and superstring combinations that exist in the array and return an array of those elements. For example − If the array is − const arr = ["abc", "abcd", "abcde", "xyz"]; Then the output should be − const output = ["abc", "abcd", "abcde"]; because the first two are the substring of the last one. How It Works The algorithm compares each string with every other string in the ...
Read MoreReturn a sorted array in lexicographical order in JavaScript
We are required to write a JavaScript function that takes two arrays, say arr1 and arr2. Our function should return a sorted array in lexicographical order of the strings of arr1 which are substrings of strings of arr2. How It Works The function iterates through arr1 and checks if each string is a substring of any string in arr2. If found, it adds the string to the result array and continues to the next string using a labeled continue statement. Example The code for this will be − const lexicographicalSort = (arr1 = [], ...
Read MoreTest for existence of nested JavaScript object key in JavaScript
Testing for the existence of nested JavaScript object keys is a common requirement when working with complex data structures. This prevents errors when accessing deeply nested properties that may not exist. The Problem Accessing nested properties directly can throw errors if intermediate keys don't exist: let test = {}; // This would throw an error: // console.log(test.level1.level2.level3); // TypeError: Cannot read property 'level2' of undefined Using a Custom Function We can create a function that safely checks each level of nesting: const checkNested = function(obj = {}){ ...
Read MoreCounting the occurrences of JavaScript array elements and put in a new 2d array
We are required to write a JavaScript function that takes in an array of literal values. The function should then count the frequency of each element of the input array and prepare a new array on that basis. For example − If the input array is − const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; Then the output should be − const output = [ [5, 3], [2, 5], [9, 1], [4, 1] ]; ...
Read More