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 79 of 534
Checking the validity of parentheses in JavaScript
We are required to write a JavaScript function that takes in a string containing just the characters: '(', ')', '{', '}', '[' and ']' Our function should determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. ...
Read MoreDash separated cartesian product of any number of arrays in JavaScript
We need to write a JavaScript function that takes any number of arrays and computes their cartesian product, with elements separated by dashes. The cartesian product combines every element from the first array with every element from the second array, and so on. What is Cartesian Product? The cartesian product of two or more sets is the set of all possible combinations where we pick one element from each set. For example, if we have arrays ['a', 'b'] and ['1', '2'], the cartesian product would be ['a-1', 'a-2', 'b-1', 'b-2']. Implementation Here's how we can implement ...
Read MoreCounting number of vowels in a string with JavaScript
We are required to write a JavaScript function that takes in a string. The function should count the number of vowels present in the string. The function should prepare an object mapping the count of each vowel against them. Example The code for this will be − const str = 'this is an example string'; const vowelCount = (str = '') => { const splitString = str.split(''); const obj = {}; const vowels = "aeiou"; splitString.forEach((letter) => { ...
Read MoreSum identical elements within one array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The array might contain some repeating / duplicate entries within it. Our function should add all the duplicate entries and return the new array thus formed. Example The code for this will be − const arr = [20, 20, 20, 10, 10, 5, 1]; const sumIdentical = (arr = []) => { let map = {}; for (let i = 0; i < arr.length; i++) { ...
Read MoreReturn the greatest possible product of n numbers from the array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should calculate and return the greatest possible product of n numbers from the array. Problem Analysis To find the maximum product of n numbers, we need to consider both positive and negative numbers. Two negative numbers multiplied together give a positive result, so the strategy involves: Sorting the array to identify the largest and smallest values Choosing ...
Read MoreReturning the highest value from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. Our function should iterate through the array and pick the greatest (largest) element from the array and return that element. Using a Custom Loop Function Here's a manual approach that iterates through the array to find the maximum value: const arr = [5, 3, 20, 15, 7]; const findGreatest = (arr = []) => { let greatest = -Infinity; if (!arr?.length) { return null; ...
Read MoreFind first duplicate item in array in linear time JavaScript
We are required to write a JavaScript function that takes in a read only array of n + 1 integers between 1 and n. The function should find one number that repeats in linear time and using at most O(n) space. For example If the input array is − const arr = [3, 4, 1, 4, 1]; Then the output should be − 4 If there are multiple possible answers (like above), we should output any one. If there is no duplicate, we should output -1. Using Set Data ...
Read MoreFinding all possible combined (plus and minus) sums of n arguments JavaScript
We need to write a JavaScript function that takes any number of arguments (all numbers) and finds the sum closest to zero from all possible combinations of addition and subtraction. For example, with arguments 1, 2, 3, the possible combinations are: 1 + 2 + 3 = 6 1 - 2 - 3 = -4 1 + 2 - 3 = 0 1 - 2 + 3 = 2 The function should return the sum closest to 0, which in this case is 0. Algorithm Approach We use a Set to store all ...
Read MoreNumber of letters in the counting JavaScript
We are required to write a JavaScript function that takes in a number, say n. The function should count the letters in the number names from 1 to n. For example − If n = 5; Then the numbers are one, two, three, four, five. And the total number of letters are 19, so the output should be 19. How It Works The algorithm uses arrays to store the letter count for each digit position and handles special cases like teens (11-19) and compound numbers (hundred, thousand). Example const sumUpto = (num = ...
Read MoreSorting numbers in descending order but with `0`s at the start JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array of numbers on the following criteria: If the array contains any zeros, they should all appear in the beginning. All the remaining numbers should be placed in a decreasing order. For example, if the input array is: const arr = [4, 7, 0, 3, 5, 1, 0]; Then after applying the sort, the array should become: const output = [0, 0, 7, 5, ...
Read More