Found 8591 Articles for Front End Technology

AND product of arrays in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:40:10

137 Views

We have an array of arrays of boolean 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.ExampleThe code for this will be −const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge = (arr = []) => {    return arr.reduce((acc, val) => {       ... Read More

Deviations in two JavaScript arrays in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:38:52

173 Views

We have two arrays of numbers like these −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];We are required to write a JavaScript function that takes in two such arrays and returns the element from arrays that are not common to both.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; const difference = (first, second) => {    const res = []; ... Read More

Upper or lower elements count in an array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:37:33

118 Views

Consider we have an array of Numbers that looks like this −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];We are required to write a function that counts how many of the elements are in the array below / above a given number.For example, if the number is 60 −The answer should be 5 elements below it (54, 54, 43, 3, 23) and 5 element par it (65, 73, 78, 76, 78) Therefore, let’s write the code for this function −ExampleThe code for this will be −const array = [54, 54, 65, 73, 43, ... Read More

Finding the mid of an array in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:11:29

3K+ Views

An Array in JavaScript is datatype which is used to store homogeneous elements. These elements are stored at contiguous memory locations. We can access each data element in the array with the help of index numbers. The index numbers will start from 0. Syntax Following is the syntax of creating a JavaScript array − const array_name = [item1, item2, ...]; The following is the simple declaration of array in JavaScript. Const Body = ['Eyes', 'Nose', 'Lips', 'Ears']; Finding the mid element of an array We are required to write a JavaScript program that finds the middlemost ... Read More

Finding sum of all unique elements in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:33:55

199 Views

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];OutputThen the output should be −const output = [2, 6, 7, 10, 4];// all the duplicate ones are summed to index 0// all the duplicate threes are summed to index 1 and so on.Therefore, let’s write the code for this function −ExampleThe code for this will be −const input = [1, 3, 1, 3, 5, 7, 5, ... Read More

Splitting an array into chunks in JavaScript

Nikhilesh Aleti
Updated on 19-Dec-2022 17:57:39

3K+ Views

In this article the given task is to split array elements into n sized chunks in JavaScript. We have some approaches to perform the splitting operation on array, those will be discussed below. Let’s assume some simple input and output scenarios – Assume there is an array containing elements in it. Example, array = [10, 20, 30, 40] and let’s take the chunk size as 2. Input = [10, 20, 30, 40] Ouput = [[10, 20], [30, 40]] The elements will be divided into two chunks, the first argument specifies the starting index and the second argument specifies the ... Read More

Similarities between different strings in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:56:30

127 Views

We have two arrays of Numbers, and we are required to write a function 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 −['world', 'you'];Approach:Had 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 ... Read More

Grouping of same kind of numbers in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:53:57

172 Views

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 nonnegative (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.So, for this array, the function should return 2.Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More

Return the sum of two consecutive elements from the original array in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:09:39

2K+ Views

An Array is known as collection of similar data elements which are stored at contiguous memory locations. Array is such a simple data structure where we can access each data element in the array with the help index numbers, which starts from 0. The following is the basic declaration of array in JavaScript − const mobiles = ["SAMSUNG", "ONEPLUS", "APPLE"]; Sum of two consecutive elements of an array 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 ... Read More

Finding pandigital numbers using JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:49:16

189 Views

We are required to write a JavaScript function that takes in a string representing a number. The function returns true if the number is pandigital, false otherwise.A pandigital number is a number that contains all digits (0-9) at least once.Therefore, let’s write the code for this function −ExampleThe code for this will be −const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => {    let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];    for(let i = 0; i < numStr.length; i++){       if(!legend.includes(numStr[i])){          continue;   ... Read More

Advertisements