Suppose, we have an array of objects like this −const arr = [ { id: '1', name: 'name 1', parentId: null }, { id: '2', name: 'name 2', parentId: null }, { id: '2_1', name: 'name 2_1', parentId: '2' }, { id: '2_2', name: 'name 2_2', parentId: '2' }, { id: '3', name: 'name 3', parentId: null }, { id: '4', name: 'name 4', parentId: null }, { id: '5', name: 'name 5', parentId: null }, { id: '6', name: 'name 6', parentId: null }, { id: '7', name: 'name 7', ... Read More
We are required to write a JavaScript function that calculates the nth root of a number and returns it.ExampleThe code for this will be −const findNthRoot = (m, n) => { try { let negate = n % 2 == 1 && m < 0; if(negate) m = −m; let possible = Math.pow(m, 1 / n); n = Math.pow(possible, n); if(Math.abs(m − n) < 1 && (m > 0 == n > 0)) return negate ? −possible : possible; } catch(e){ return null; } }; console.log(findNthRoot(45, 6));OutputAnd the output in the console will be −1.8859727740585395
We are required to write a JavaScript function that takes in a number. The function should divide the number into chunks according to the following rules −The number of chunks should be a power−of−two, Each chunk should also have a power-of-two number of items (where size goes up to a max power of two, so 1, 2, 4, 8, 16, 32, 32 being the max)Therefore, for example, 8 could be divided into 1 bucket −[8]9 could be −[8, 1]That works because both numbers are powers of two, and the size of the array is 2 (also a power of two).Let's ... Read More
Suppose, we have an array of objects containing some data about some users like this −const arr = [ { "name":"aaa", "id":"2100", "designation":"developer" }, { "name":"bbb", "id":"8888", "designation":"team lead" }, { "name":"ccc", "id":"6745", "designation":"manager" }, { "name":"aaa", "id":"9899", "designation":"sw" } ];We are required to write a JavaScript function that takes in one such array. Then our ... Read More
Suppose, we have an array of two numbers that specify a range. We are required to write a function that finds the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.The range will be an array of two numbers that will not necessarily be in numerical order.For example, if given [1, 3], then we are required to find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here ... Read More
Suppose, we have a JSON array of objects like this −const arr = [ { "id": "03868185", "month_10": 6, }, { "id": "03870584", "month_6": 2, }, { "id": "03870584", "month_7": 5, }, { "id": "51295", "month_1": 1, }, { "id": "51295", "month_10": 1, }, { "id": "55468", "month_11": 1, } ];Here, we ... Read More
We are required to write a JavaScript function that takes in two sorted array of numbers. The function should merge the two arrays together to form a resultant sorted array and return that array.For example −If the two arrays are −const arr1 = [2, 6, 6, 8, 9]; const arr2 = [1, 4, 5, 7];Then the output array should be −const output = [1, 2, 4, 6, 6, 7, 8, 9];ExampleThe code for this will be −const arr1 = [2, 6, 6, 8, 9]; const arr2 = [1, 4, 5, 7]; const mergeSortedArrays = (arr1 = [], arr2 = []) ... Read More
Suppose, we have two arrays of objects like these −const arr1 = [ {name:'test', lastname: 'test', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'} ]; const arr2 = [ {name:'test21', lastname: 'test21', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'}, {name:'test22', lastname: 'test22', gender:'m'} ];These arrays do not have any repeating objects within (repeating on the basis of 'name' property) but there exist some objects with repeating names in the first and second objects.We are required to write a JavaScript function that takes two such arrays and returns a new array.The ... Read More
We are required to write a JavaScript function that takes in a number as the only argument. The function should then return a random generated number which is always divisible by the number provided by the argument.ExampleThe code for this will be −const num = 21; // function that generates random numbers divisible by n with a default upper limit of 1000000 const specialRandom = (num = 1, limit = 1000000) => { // getting a random number const random = Math.random() * limit; // rounding it off to be divisible by num const res = ... Read More
We are required to write a JavaScript function that takes in an array of string literals as the first argument and a single string character as the second argument.Then our function should find and return the first array entry that starts with the character specified by the second argument.ExampleThe code for this will be −const names = ['Naman', 'Kartik', 'Anmol', 'Rajat', 'Keshav', 'Harsh', 'Suresh', 'Rahul']; const firstIndexOf = (arr = [], char = '') => { for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(el.substring(0, 1) === char){ ... Read More