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 169 of 801
AND product of arrays in JavaScript
We have an array of arrays of boolean values like this: const arr = [[true, false, false], [false, false, false], [false, false, true]]; We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator. Let's write the code for this function. We will be using Array.prototype.reduce() function to achieve this. Example The code for this will be: const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge ...
Read MoreKeeping only alphanumerals in a JavaScript string in JavaScript
We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string that has all special characters replaced with their corresponding ASCII value, keeping only alphanumeric characters and spaces in their original form. Therefore, let's write the code for this function: Example The code for this will be: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str => { let res = ''; for(let i ...
Read MoreDetecting the first non-unique element in array in JavaScript
We are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. We have to do this in constant space (i.e., without utilizing extra memory). So, let's write the solution for this problem. We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicacy. How It Works The algorithm works by comparing each element's current index with its last occurrence index using lastIndexOf(). ...
Read MoreInserting empty string in place of repeating values in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. For example: If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. Therefore, let's write the code for this function − Example The code for this will be − const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => { ...
Read MoreCumulative average of pair of elements in JavaScript
We have an array of numbers and need to write a function that returns an array with the average of each element and its predecessor. For the first element, since there's no predecessor, we return the element itself. Let's implement this using the Array.prototype.map() method to transform each element based on its position. How It Works The algorithm works as follows: For the first element (index 0): return the element itself For other elements: calculate (current + previous) / 2 Example const arr = [3, 5, 7, 8, 3, 5, 7, ...
Read MoreExpanding Numerals in JavaScript
We are required to write a function that, given a number, say, 123, will output an array − [100, 20, 3] Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function. Understanding Place Values Each digit in a number has a place value based on its position. For example, in 123: 1 is in the hundreds place (1 × 100 = 100) 2 is in the ...
Read MoreFiltering array to contain palindrome elements in JavaScript
We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. For example If the input array is − const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be − const output = [12321, 'did']; We will create a helper function that takes in a number or a string and checks if it's a palindrome or not. Then we will loop over the array, filter ...
Read MoreSorting a JSON object in JavaScript
In JavaScript, objects themselves cannot be sorted since their properties don't have a guaranteed order. However, we can extract the values from a JSON object and sort them into an array. Suppose we have an object like this: const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; We need to write a JavaScript function that takes this object and returns a sorted array of its values: const arr = [11, 23, 56, ...
Read MoreConstructing an object from repetitive numeral string in JavaScript
Suppose, we have a string with digits like this − const str = '11222233344444445666'; We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string. So, for this string, the output should be − const output = { "1": 2, "2": 4, "3": 3, "4": 7, "5": 1, "6": 3 }; Using a for Loop The most straightforward approach is to iterate ...
Read MoreFinding upper elements in array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument. Therefore, let's write the code for this function — Using for Loop The most straightforward approach uses a for loop to iterate through the array and check each element: const arr = [56, 34, 2, 7, 76, 4, 45, 3, ...
Read More