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
Front End Technology Articles
Page 214 of 652
Return indexes of greatest values in an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. The array may contain more than one greatest element (i.e., repeating greatest element). We are required to write a JavaScript function that takes in one such array and returns all the indices of the greatest element. Using Array.reduce() Method This approach first finds the maximum value using Math.max(), then uses reduce() to collect all indices where the element equals the maximum value. const arr = [10, 5, 4, 10, 5, 10, 6]; const findGreatestIndices = arr => { ...
Read MoreSplitting number into n parts close to each other in JavaScript
We are required to write a JavaScript function that takes in a number, num, as the first argument and another number, parts, as the second argument. Our function should split the number num into exactly parts numbers while keeping these conditions in mind: The numbers should be as close as possible The numbers should be even (if possible) The ordering of numbers is not important. Problem Analysis To split a number into n parts as evenly as possible, we need to: Calculate ...
Read MoreUse array as sort order in JavaScript
In JavaScript, you can sort an array of objects based on a custom order defined by another array. This is useful when you need to arrange data according to a specific sequence rather than alphabetical or numerical order. Problem Statement Given a reference array that defines the desired order and an array of objects, we need to sort the objects so their property values match the sequence in the reference array. const sort = ["this", "is", "my", "custom", "order"]; const myObjects = [ {"id":1, "content":"is"}, {"id":2, "content":"my"}, ...
Read MoreAlternatingly combining array elements in JavaScript
We are required to write a JavaScript function that takes in any number of arrays of literals as input. Our function should prepare a new array that contains elements picked alternatingly from all the input arrays. For example, if the input to the function is − Problem Input const arr1 = [1, 2, 3, 4]; const arr2 = [11, 12, 13, 14]; const arr3 = ['a', 'b', 'c']; Expected Output const output = [1, 11, 'a', 2, 12, 'b', 3, 13, 'c', 4, 14]; The function should take ...
Read MoreChecking if a key exists in a JavaScript object
We are required to illustrate the correct way to check whether a particular key exists in an object or not. Before moving on to the correct way let's first examine an incorrect way and see how it's actually incorrect. Way 1: Checking for undefined value (incorrect way) Due to the volatile nature of JavaScript, we might want to check for the existence of key in an object like this: const obj = { name: 'Rahul' }; // Incorrect approaches console.log(!obj['fName']); ...
Read MorePlacing integers at correct index in JavaScript
We need to write a JavaScript function that takes a string containing only square brackets '[' and ']' and determines the minimum number of brackets to add to make it balanced. Problem Statement Given a string consisting of only '[' and ']' characters, find the minimum number of brackets that need to be added to make the string valid (properly balanced). A valid bracket string means every opening bracket '[' has a corresponding closing bracket ']' that comes after it. Example Input and Output Input: const str = '[]]'; Output: 1 ...
Read MoreExtract arrays separately from array of Objects in JavaScript
Suppose, we have an array of objects like this − const arr = [{ name : 'Client 1', total: 900, value: 12000 }, { name : 'Client 2', total: 10, value: 800 }, { name : 'Client 3', total: 5, value : 0 }]; We are required to write a JavaScript function that takes in one such array and extracts a separate array for each object property. Therefore, ...
Read MoreFinding score of brackets in JavaScript
We are required to write a JavaScript function that takes in a balanced square bracket string as an argument and computes its score based on specific rules. Scoring Rules The bracket scoring follows these rules: [] has score 1 AB has score A + B, where A and B are balanced bracket strings [A] has score 2 * A, where A is a balanced bracket string Example Input and Output For the input string '[][]': Input: '[][]' Output: 2 This works because [] scores 1, and [][] is two ...
Read MoreFilter an array containing objects based on another array containing objects in JavaScript
Suppose we have two arrays of objects like these − const arr1 = [{id:'1', name:'A'}, {id:'2', name:'B'}, {id:'3', name:'C'}, {id:'4', name:'D'}]; const arr2 = [{id:'1', name:'A', state:'healthy'}, {id:'3', name:'C', state:'healthy'}]; We are required to write a JavaScript function that takes in two such arrays. Our function should return a new filtered version of the first array (arr1 in this case) that contains only those objects with a name property that are not contained in the second array (arr2 in this case) with the same name property. Therefore, the output, in this case, should look like ...
Read MoreCount and return the number of characters of str1 that makes appearances in str2 using JavaScript
We need to write a JavaScript function that takes two strings as parameters and counts how many characters from the first string appear in the second string, including duplicate occurrences. Problem Statement Given two strings str1 and str2, count how many characters from str1 also appear in str2. If a character appears multiple times in str2, count each occurrence separately. Example: str1 = 'Kk' contains characters 'K' and 'k' str2 = 'klKKkKsl' contains: k(1), l(1), K(2), K(3), k(4), K(5), s(6), l(7) Characters from str1 found in str2: k, K, K, k, K = 5 matches ...
Read More