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 215 of 801
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 MoreConverting alphabets to Greek letters in JavaScript
Converting alphabets to Greek letters in JavaScript requires creating a mapping between English and Greek characters, then replacing each character accordingly. Character Mapping First, let's establish the mapping between English and Greek letters: A=α (Alpha) B=β (Beta) D=δ (Delta) E=ε (Epsilon) I=ι (Iota) K=κ (Kappa) N=η (Eta) O=θ (Theta) P=ρ (Rho) R=π (Pi) T=τ (Tau) U=μ (Mu) V=υ (Upsilon) W=ω ...
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 MoreComputing Ackerman number for inputs in JavaScript
The Ackermann Function is a classic example of a recursive function that grows extremely quickly. It's notable for being a total computable function that is not primitive recursive, making it an important concept in theoretical computer science. Problem Statement We need to write a JavaScript function that takes two non-negative integers, m and n, and returns the Ackermann number A(m, n) defined by the following mathematical definition: A(m, n) = n+1 if m=0 A(m, n) = A(m-1, 1) if m>0 and n=0 A(m, n) = A(m-1, A(m, n-1)) if m>0 and n>0 Implementation ...
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 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 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 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 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 MoreIterate through Object keys and manipulate the key values in JavaScript
When working with arrays of objects in JavaScript, you often need to iterate through object keys and transform their values. This article demonstrates how to extract specific elements from array values within objects. Problem Statement Suppose we have an array of objects where each property contains an array of values: const arr = [ { col1: ["a", "b"], col2: ["c", "d"] }, { ...
Read More