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 338 of 534
Summing all the unique values of an array - JavaScript
Let’s say, we are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index.For example −If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];Then the output should be −const output = [2, 6, 7, 10, 4];That means all the duplicate ones are summed to index 0 and all the duplicate threes are summed to index 1 and so on.ExampleFollowing is the code −const input = [1, 3, 1, 3, 5, 7, 5, 4]; const mergeDuplicates = arr => ...
Read MoreDividing an array – JavaScript
Let’s say, we are required to write a function that takes in an array arr of string / number literals as the first argument and a number n as second argument.We are required to return an array of n subarrays, each of which contains at most arr.length / n elements. And the distribution of elements should be like this −The first element goes in the first subarray, second in second, third in third and so on.Once we have one element in each subarray, we again start with filling the first subarray with its second element.Similarly, when all subarrays have two ...
Read MoreFinding the intersection of arrays of strings - JavaScript
We have two arrays of Numbers, and we are required to write a function, let’s say intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example −If input is −arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate'];Then the output should be −Output: ['world', 'you'];ApproachHad the arrays been sorted, we could have used the two pointer approach with initially both pointing to 0 the start of the respective array and we ...
Read MoreCounting the clusters of positive numbers - JavaScript Arrays
Let’s say, we have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];We are required to write a JavaScript function that counts the consecutive groups of non-negative (positives and 0) numbers in the array.Like here we have consecutive non-negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from 9 to end of array forms the second group.Therefore, for this array, the function should return 2.ExampleFollowing is the code −const arr = [-1, -2, -1, 0, -1, -2, -1, ...
Read MoreAdding two values at a time from an array - JavaScript
Let’s say, we are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from the original array.For example, if the input array is −const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8];Then the output should be −const output = [9, 90, 26, 4, 14];ExampleFollowing is the code −const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; const twiceSum = arr => { const res = []; for(let i = 0; i < arr.length; i += 2){ res.push(arr[i] + (arr[i+1] || 0)); }; return res; }; console.log(twiceSum(arr));OutputThis will produce the following output in console −[ 9, 90, 26, 4, 14 ]
Read MoreSplitting last n digits of each value in the array in JavaScript
We have an array of literals like this −const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799];We are required to write a JavaScript function that takes in this array and a number n and if the corresponding element contains more than or equal to n characters, then the new element should contain only the last n characters otherwise the element should be left as it is.Therefore, if n = 2, for this array, the output should be −const output = [68, 65, 67, 3, 78, 78, 35, 99];ExampleFollowing is the code −const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; const splitLast = (arr, num) => { return arr.map(el => { if(String(el).length
Read MoreMatching strings for similar characters - JavaScript
We are required to write a JavaScript function that accepts two string and a number n.The function matches the two strings i.e., it checks if the two strings contains the same characters.The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false.ExampleFollowing is the code −const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => { let count = 0; for(let i = 0; i < first.length; i++){ ...
Read MoreFunction that parses number embedded in strings - JavaScript
Conventionally, we have functions like parseInt() and parseFloat() that takes in a string and converts the number string to Number. But these methods fail when we have numbers embedded at random index inside the string.For example: The following would only return 454, but what we want is 4545453 −parseInt('454ffdg54hg53')So, we are required to write a JavaScript function that takes in such string and returns the corresponding number.ExampleFollowing is the code −const numStr = '454ffdg54hg53'; const parseInteger = numStr => { let res = 0; for(let i = 0; i < numStr.length; i++){ if(!+numStr[i]){ ...
Read MoreHow to access variables declared in a function, from another function using JavaScript?
We have to write a function, that does some simple task, say adding two numbers or something like that. We are required to demonstrate the way we can access the variables declared inside that function in some other function or globally.ExampleFollowing is the code −const num = 5; const addRandomToNumber = function(num){ // a random number between [0, 10) const random = Math.floor(Math.random() * 10); // assigning the random to this object of function // so that we can access it outside this.random = random; this.res = num + random; }; const addRandomInstance = ...
Read MoreIf ([] == false) is true, why does ([] || true) result in []? - JavaScript
If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following −In the first case, we are using loose conditional checking, allowing type coercion to take over.While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hook.Let's now unveil the conversions that happens behind the scenes in both cases.Case 1 − ([] == false)According to the MDN docs if two data types say x and y are compared using the loose ...
Read More