We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a target sum Number as the second argument.The function should return an array of all those subarrays from the original array whose elements sum to make the target sum. We can use a single number twice to achieve the sum.For example −If the input array and number are −const arr = [1, 2, 4]; const sum = 4;then the output should be −const output = [ [1, 1, 1, 1], [1, 1, 2], [2, 2], [4] ... Read More
Suppose, we have an array of objects like this −const arr = [ {"id":7, "name":"Kuwait", "parentId":2}, {"id":4, "name":"Iraq", "parentId":2}, {"id":10, "name":"Qatar", "parentId":2}, {"id":2, "name":"Middle East", "parentId":1}, {"id":3, "name":"Bahrain", "parentId":2}, {"id":6, "name":"Jordan", "parentId":2}, {"id":8, "name":"Lebanon", "parentId":2}, {"id":1, "name":"Africa/Middle East", "parentId":null}, {"id":5, "name":"Israel", "parentId":2}, {"id":9, "name":"Oman", "parentId":2} ];We are required to write a JavaScript function that takes in one such array. The function should then prepare a new array that contains the objects grouped according to their parents.Therefore, the output should look like this −const output = [ ... Read More
Suppose, we have two arrays, let’s say arr1 and arr2. The elements of arr2 are distinct, and all elements in arr2 are also in arr1.We are required to write a JavaScript function that takes in two such arrays and sorts the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.For example− If the two input arrays are −const arr1 = [2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19]; const arr2 = ... Read More
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The function should then return the length of the longest continuous subarray from the array that only contains elements in a strictly increasing order.A strictly increasing sequence is the one in which any succeeding element is greater than all its preceding elements.Exampleconst arr = [5, 7, 8, 12, 4, 56, 6, 54, 89]; const findLongest = (arr) => { if(arr.length == 0) { return 0; }; let max = 0; let count ... Read More
The degree of an array of literals is defined as the maximum frequency of any one of its elements.const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3];The degree of this array is 4, because 3 is repeated 4 times in this array.We are required to write a JavaScript function that takes in an array of literals. The task of our function is to find the length of the smallest continious subarray from the array whose degree is same as of the whole array.Exampleconst arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3]; const ... Read More
Suppose we have an array of objects like this −const arr = [ {userId: "3t5bsFB4PJmA3oTnm", from: 1, to: 6}, {userId: "3t5bsFB4PJmA3oTnm", from: 7, to: 15}, {userId: "3t5bsFB4PJmA3oTnm", from: 172, to: 181}, {userId: "3t5bsFB4PJmA3oTnm", from: 182, to: 190} ];We are required to write a JavaScript function that takes in one such array. The function should group overlapping objects based on their "from" and "to" property into a single object like this −const output = [ {userId: "3t5bsFB4PJmA3oTnm", from: 1, to: 15}, {userId: "3t5bsFB4PJmA3oTnm", from: 172, to: 190} ];Exampleconst arr = [ {userId: "3t5bsFB4PJmA3oTnm", from: ... Read More
We are required to write a JavaScript function that in a positive integer, say n. The function should find and return the longest distance between any two adjacent 1's in the binary representation of n.If there are no two adjacent 1's, then we have to return 0.Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.For example −If the input is 22, then the output should be 2, because, The binary ... Read More
We are required to write a JavaScript function that takes in an array of literals as the first argument and a search string as the second argument.The function should the array for the that search string. If that string exists in the array, we should return its next element from the array, otherwise we should return false.Exampleconst arr = ["", "comp", "myval", "view", "1"] const getNext = (value, arr) => { const a = [undefined].concat(arr) const p = a.indexOf(value) + 1; return a[p] || false; } console.log(getNext('comp', arr)); console.log(getNext('foo', arr));OutputAnd the output in the console will be ... Read More
We are required to write a program (function) that takes in an object reference and returns an array of all the methods (member functions) that lives on that object.We are only required to return the methods in the array and not any other property that might have value of type other than a function.We will use the Object.getOwnPropertyNames functionThe Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object. And then we will filter the array to contain property of data type 'function' only.Exampleconst returnMethods = (obj = {}) => { const ... Read More
Suppose, we have an object like this −const obj = { "firstName": "John", "lastName": "Green", "car.make": "Honda", "car.model": "Civic", "car.revisions.0.miles": 10150, "car.revisions.0.code": "REV01", "car.revisions.0.changes": "", "car.revisions.1.miles": 20021, "car.revisions.1.code": "REV02", "car.revisions.1.changes.0.type": "asthetic", "car.revisions.1.changes.0.desc": "Left tire cap", "car.revisions.1.changes.1.type": "mechanic", "car.revisions.1.changes.1.desc": "Engine pressure regulator", "visits.0.date": "2015-01-01", "visits.0.dealer": "DEAL-001", "visits.1.date": "2015-03-01", "visits.1.dealer": "DEAL-002" };We are required to write a JavaScript function that takes in one such object and unflattens it into nested objects and arrays.Therefore, ... Read More