Found 6710 Articles for Javascript

Convert 2D array to object using map or reduce in JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:56:41

1K+ Views

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 More

How to multiply odd index values JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:52:37

380 Views

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 More

Find char combination in array of strings JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:51:37

134 Views

We have to write a function that accepts an array of strings and a string. Our job is to check whether the array contains any sequence or subsequence of the string as its element or not, and the function should return a boolean based on this fact.For instance −const x = 'ACBC'; const arr = ['cat', 'AB']; const arr2 = ['cat', '234', 'C']; const arr3 = ['cat', 'CC']; const arr4 = ['cat', 'BB']; console.log(containsString(arr, x)) // true console.log(containsString(arr2, x)) // true console.log(containsString(arr3, x)) // true console.log(containsString(arr4, x)) // falseTherefore, let’s write the code for this function −Exampleconst x = 'ACBC'; ... Read More

Calculating least common of a range JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:49:34

136 Views

We are required to write a function that takes in an array of two numbers a and b (a >= b) and returns the least common multiple of all the numbers between [a, b].ApproachWe will first write a basic function that calculates the least common multiple of two numbers, once we have that we will recursively call it over the numbers that fall between [a, b] and finally return the result.Exampleconst lcm = (a, b) => {    let min = Math.min(a, b);    while(min >= 2){       if(a % min === 0 && b % min === ... Read More

Write a program to calculate the least common multiple of two numbers JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:46:45

855 Views

We are required to write a function that accepts two numbers and returns their least common multiple.Least Common Multiple (LCM)The least common multiple of two numbers a and b is the smallest positive integer that is divisible by both a and b.For example − The LCM of 6 and 8 is 24 because 24 is the smallest positive integer that is divided by both 6 and 8.Method to calculate LCMOne of the many ways of calculating LCM of two numbers a and b is by dividing the product of a and b by the greatest integer (also known as greatest ... Read More

map() array of object titles into a new array based on other property value JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:44:54

234 Views

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 More

JavaScript equivalent of Python's zip function

Alshifa Hasnain
Updated on 14-Jan-2025 03:27:00

1K+ Views

In this article, we will learn the JavaScript equivalent of Python's zip Function In Python, the zip() function is a convenient way to combine multiple tables into a single iterable of tuples. However, JavaScript does not have a built-in equivalent for this functionality Problem Statement Given two or more arrays of equal length, implement a function in JavaScript that combines these arrays into a single variety of arrays, where each inner array contains the elements from the input arrays at the corresponding positions. Input arr1 = [1, 2, 3], arr2 = ['a', 'b', 'c']Output [[1, 'a'], [2, 'b'], [3, 'c']] ... Read More

Construct string via recursion JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:41:33

328 Views

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 More

Find and return array positions of multiple values JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:40:06

3K+ Views

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

How can I remove a specific item from an array in JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:38:31

265 Views

We are required to write a function for arrays Array.prototype.remove(). It accepts one argument; it is either a callback function or a possible element of the array. If it’s a function then the return value of that function should be considered as the possible element of the array and we have to find and delete that element from the array in place and the function should return true if the element was found and deleted otherwise it should return false.Therefore, let’s write the code for this function −Exampleconst arr = [12, 45, 78, 54, 1, 89, 67]; const names = ... Read More

Advertisements