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 427 of 534
Get minimum number without a Math function JavaScript
We have to write a function that takes in n Number literals as argument, where n is any whole number and returns the smallest number of those numbers without using any library function.We will solve this problem via a while loop and the code for this will be −Exampleconst numbers = [12, 5, 7, 43, -32, -323, 5, 6, 7, 767, 23, 7]; const findMin = (...numbers) => { let min = Infinity, len = 0; while(len++ < numbers.length){ min = numbers[len] < min ? numbers[len] : min; } return min; }; console.log(findMin(...numbers));OutputThe output in the console will be −-323
Read MoreCounting how many times an item appears in a multidimensional array in JavaScript
We have a nested array of strings and we have to write a function that accepts the array and a search string and returns the count of the number of times that string appears in the nested array.Therefore, let’s write the code for this, we will use recursion here to search inside of the nested array and the code for this will be −Exampleconst arr = [ "apple", ["banana", "strawberry", "dsffsd", "apple"], "banana", ["sdfdsf", "apple", ["apple", ["nonapple", "apple", ["apple"]]]] , "apple"]; const calculateCount = (arr, query) => { let count = ...
Read MoreIntersection of two arrays JavaScript
We have two arrays of Numbers, and we are required to write a function, 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: arr1 = [1, 2, 3, 1], arr2 = [1, 3, 1] Output: [1, 3, 1]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 could have proceeded with increasing the corresponding ...
Read MoreHow to find a group of three elements in an array whose sum equals some target sum JavaScript
We have to write a function, say threeSum() that takes in an array of Numbers and a target sum. It checks whether there exist any three numbers in the array that add up to the target sum, if there exist such three numbers in the array, then it should return their indices in an array otherwise it should return -1.ApproachThe approach is simple, We will first write a function twoSum(), that takes in an array and a target sum and takes linear time and space to return the indices of two numbers that add up to target sum otherwise -1.Then ...
Read MoreConvert array of arrays to array of objects grouped together JavaScript
Let’s say, we have a two-dimensional array that contains data about some colors and fruits like thisconst data = [ ['orange', 'fruit'], ['red', 'color'], ['green', 'color'], ['orange', 'color'], ['banana', 'fruit'], ['blue', 'color'], ['lemon', 'fruit'], ['mango', 'fruit'], ['lemon', 'color'], ];We have to write a function that takes in this array and returns an array in which the different fruits and colors are grouped by their categories.Like in this example we only have two categories namely ‘fruit’ and ‘color’, so we should expect an array of two objects in the output like this ...
Read MoreConvert 2D array to object using map or reduce in JavaScript
Let’s say, we have a two-dimensional array that contains some data about the age of some people.The data is given by the following 2D arrayconst data = [ ['Rahul', 23], ['Vikky', 27], ['Sanjay', 29], ['Jay', 19], ['Dinesh', 21], ['Sandeep', 45], ['Umesh', 32], ['Rohit', 28], ];We are required to write a function that takes in this 2-D array of data and returns an object with key as the first element of each subarray i.e., the string and value as the second element.We will use the Array.prototype.reduce() method to construct this object, and the ...
Read MoreHow to multiply odd index values JavaScript
We are required to write a function, that takes in an array of Number literals as one and the only argument. The numbers that are situated at even index should be returned as it is. But the numbers situated at the odd index should be returned multiplied by their corresponding indices.For example −If the input is: [5, 10, 15, 20, 25, 30, 50, 100] Then the function should return: [5, 10, 15, 60, 25, 150, 50, 700]We will use the Array.prototype.reduce() method to construct the required array and the code for the function will be −Exampleconst arr = ...
Read Moremap() array of object titles into a new array based on other property value JavaScript
Let’s say, we have an array of objects like this −const arr = [{ country: "cananda", count: 2 }, { country: "jamaica", count: 2 }, { country: "russia", count: 1 }, { country: "india", count: 3 }, { country: "spain", count: 2 }, { country: "portugal", count: 1 }, { country: "italy", count: 1 }];We are required to write a function that ...
Read MoreConstruct string via recursion JavaScript
We are required to write a recursive function, say pickString that takes in a string that contains a combination of alphabets and numbers and returns a new string consisting of only alphabets.For example, If the string is ‘dis122344as65t34er’, The output will be: ‘disaster’Therefore, let’s write the code for this recursive function −Exampleconst str = 'ex3454am65p43le'; const pickString = (str, len = 0, res = '') => { if(len < str.length){ const char = parseInt(str[len], 10) ? '' : str[len]; return pickString(str, len+1, res+char); }; return res; }; console.log(pickString(str)); console.log(pickString('23123ca43n y43ou54 6do884 ...
Read MoreFind and return array positions of multiple values JavaScript
We have to write a function, say findPositions() that takes in two arrays as argument. And it should return an array of the indices of all the elements of the second array present in the first array.For example −If the first array is [‘john’, ‘doe’, ‘chris’, ‘snow’, ‘john’, ‘chris’], And the second array is [‘john’, chris]Then the output should be −[0, 2, 4, 5]Therefore, let’s write the code for this function. We will use a forEach() loop here;Exampleconst values = ['michael', 'jordan', 'jackson', 'michael', 'usain', 'jackson', 'bolt', 'jackson']; const queries = ['michael', 'jackson', 'bolt']; const findPositions = (first, second) => ...
Read More