Found 6683 Articles for Javascript

Relative sorting in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:42:38

214 Views

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

Longest subarray which only contains strictly increasing numbers JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:41:24

201 Views

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

Finding degree of subarray in an array JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:40:03

479 Views

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

Create array from JSON object JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:38:30

1K+ Views

Suppose, we have the following JSON object −const obj = {    "test1": [{       "1": {       "rssi": -25,    }    }, {       "2": {          "rssi": -25,       }    }],    "test2": [{       "15": {          "rssi": -10,       } }, {          "19": {          "rssi": -21,       }    }] };We are required to write a JavaScript function that takes in an object like this −The ... Read More

Complicated array grouping JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:36:36

133 Views

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

Longest distance between 1s in binary JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:35:12

297 Views

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

Check if a value exists in an array and get the next value JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:33:42

1K+ Views

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

Get all methods of any object JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:32:48

1K+ Views

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

How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?

AmitDiwan
Updated on 21-Nov-2020 06:31:15

445 Views

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

Find the least duplicate items in an array JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:26:11

308 Views

We are required to write a JavaScript function that takes in an array of literals which may contain some duplicate values.The function should return an array of all those elements that are repeated for the least number of times.For example− If the input array is −const arr = [1, 1, 2, 2, 3, 3, 3];Then the output should be −const output = [1, 2];because 1 and 2 are repeated for the least number of times (2)Exampleconst arr = [1, 1, 2, 2, 3, 3, 3]; const getLeastDuplicateItems = (arr = []) => {    const hash = Object.create(null);    let ... Read More

Advertisements