Found 8591 Articles for Front End Technology

Counting below / par elements from an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:31:13

183 Views

We are required to write a function that counts how many of the elements are in the array below / above a given number.Following is our array of Numbers −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];For example, if the number is 60, the answer should be five elements below it −54, 54, 43, 3, 23and five element par it −65, 73, 78, 76, 78ExampleFollowing is the code −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; const belowParNumbers = (arr, num) => {    return arr.reduce((acc, ... Read More

Code to find the center of an array without using ES6 functions - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:30:04

330 Views

We are required to write an array function midElement() that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops.If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements.ExampleFollowing is the code −const arr = [14, 32, 36, 42, 45, 66, 87]; const array = [13, 92, 83, 74, 55, 46, 74, 82]; const midElement = (arr, ind = 0) => {    if(arr[ind]){       ... Read More

Summing all the unique values of an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:29:05

288 Views

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 More

Dividing an array – JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:27:30

606 Views

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 More

Finding the intersection of arrays of strings - JavaScript

AmitDiwan
Updated on 18-Sep-2020 10:02:47

749 Views

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 More

Counting the clusters of positive numbers - JavaScript Arrays

AmitDiwan
Updated on 18-Sep-2020 10:01:40

270 Views

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 More

Adding two values at a time from an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 10:00:25

2K+ Views

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 ]

Splitting last n digits of each value in the array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:58:01

100 Views

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

Odd even index difference - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:55:08

775 Views

We are required to write a JavaScript function that takes in an array of numbers like this −const arr = [3, 6, 34, 12, 6, 8, 8, 5, 6, 8];The function should return the difference between the sum of elements present at the odd index and the sum of elements present at even indexExampleFollowing is the code −const arr = [3, 6, 34, 12, 6, 8, 8, 5, 6, 8]; const oddEvenDiff = arr => {    let diff = 0;    for(let i = 0; i < arr.length; i++){       if(i % 2 === 0){          diff += arr[i];       }else{          diff -= arr[i]       };    };    return Math.abs(diff); }; console.log(oddEvenDiff(arr));OutputThis will produce the following output in console −18

Matching strings for similar characters - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:53:31

574 Views

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 More

Advertisements