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
Javascript Articles
Page 281 of 534
Return 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.ExampleThe code for this will be −const lexicographicalSort = (arr1 = [], arr2 = []) => { let i, j; const res = []; outer: for (j = 0; j < arr1.length; j++) { for (i = 0; i < arr2.length; i++) { if (arr2[i].includes(arr1[j])) { res.push(arr1[j]); ...
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 last.ExampleThe code for this will be −const arr = ["abc", "abcd", "abcde", "xyz"]; const findStringCombinations = (arr = []) => { let i, j, res = {}; for (i = 0; ...
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.const arr = ['25', '50', 'a', 'a', 'b', 'c']In this case, we should return 'a'const arr = ['75', '100', 'a', 'b', 'b', 'a']In this case, I should also get 'a'ExampleThe code for this will be −const arr = ['25', '50', 'a', 'a', 'b', 'c']; const arr1 = ['75', '100', 'a', 'b', 'b', 'a']; const getMostFrequentValue = ...
Read MoreSort an array of objects by multiple properties in JavaScript
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 }, { id: 5, score: 5, isCut: true, dnf: true }, { id: 6, score: 6, isCut: true, dnf: false }, { id: 7, score: 7, isCut: true, dnf: false }, { id: 8, score: 8, isCut: true, dnf: false ...
Read MoreHow to convert nested array pairs to objects in an array in JavaScript ?
Suppose, we have an array of arrays like this −const arr = [ [ ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'], [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] ] ] ];We are required to write a JavaScript function that takes in one such array. The function should construct an array of objects based on this array of arrays.The output array should ...
Read MoreHow to check if an array contains integer values in JavaScript ?
We are required to write a JavaScript function that takes in an array of elements.Our function should check whether or not the array contains an integer value.We should return true if it does false otherwise.ExampleThe code for this will be −const arr = ["123", "", "21345", "90"]; const findInteger = (arr = []) => { const isInteger = num => { return typeof num === 'number'; }; const el = arr.find(isInteger); return !!el; }; console.log(findInteger(arr));OutputAnd the output in the console will be −false
Read MoreSplit number into 4 random numbers in JavaScript
We are required to write a JavaScript function that takes in a number as the first input and a maximum number as the second input.The function should generate four random numbers, which when summed should equal the number provided to function as the first input and neither of those four numbers should exceed the number given as the second input.For example − If the arguments to the function are −const n = 10; const max = 4;Then, const output = [3, 2, 3, 2];is a valid combination.Note that repetition of numbers is allowed.ExampleThe code for this will be −const total ...
Read MoreHow to build a string with no repeating character n separate list of characters? in JavaScript
Suppose, we n separate array of single characters. We are required to write a JavaScript function that takes in all those arrays.The function should build all such possible strings that −contains exactly one letter from each arraymust not contain any repeating character (as the arrays might contain common elements)For the purpose of this problem, we will consider these three arrays, but we will write our function such that it works well with variable number of arrays −const arr1 = [a, b ,c, d ]; const arr2 = [e, f ,g ,a]; const arr3 = [m, n, o, g, k];ExampleThe code ...
Read MoreCheck if the string is a combination of strings in an array using JavaScript
We are required to write a JavaScript function that takes in an array of strings as the first argument and a string as the second argument.The function should check whether the string specified by second argument can be formed by combining the strings of the array in any possible way.For example − If the input array is −const arr = ["for", "car", "keys", "forth"];And the string is −const str = "forthcarkeys";Then the output should be true, because the string is a combination of elements at 3, 1 and 2 indexes of the array.ExampleThe code for this will be −const arr ...
Read MoreSubset with maximum sum in JavaScript
We are required to write a JavaScript function that takes in an array of integers. Our function is required find the subset of non−adjacent elements with the maximum sum.And finally, the function should calculate and return the sum of that subset.For example −If the input array is −const arr = [3, 5, 7, 8, 10];Then the output should be 20 because the non−adjacent subset of numbers will be 3, 7 and 10.ExampleThe code for this will be −const arr = [3, 5, 7, 8, 10]; const maxSubsetSum = (arr = []) => { let min = −Infinity const ...
Read More