Suppose we have two arrays of objects like these −const arr1 = [{id:'1', name:'A'}, {id:'2', name:'B'}, {id:'3', name:'C'}, {id:'4', name:'D'}]; const arr2 = [{id:'1', name:'A', state:'healthy'}, {id:'3', name:'C', state:'healthy'}];We are required to write a JavaScript function that takes in two such arrays. Our function should return a new filtered version of the first array (arr1 in this case) that contains only those objects with a name property that are not contained in the second array (arr2 in this case) with the same name property.Therefore, the output, in this case, should look like −const output = [{id:'2', name:'B'}, {id:'4', name:'D'}];ExampleThe code ... Read More
Suppose, we have an array of objects like this −const arr = [{ name : 'Client 1', total: 900, value: 12000 }, { name : 'Client 2', total: 10, value: 800 }, { name : 'Client 3', total: 5, value : 0 }];We are required to write a JavaScript function that takes in one such array and extracts a separate array for each object property.Therefore, one array for the name property of each object, one for total and one for value. If there existed more properties, we would have separated more ... Read More
We are required to illustrate the correct way to check whether a particular key exists in an object or not. Before moving on to the correct way let's first examine an incorrect way and see how actually its incorrect.Way 1: Checking for undefined value (incorrect way)Due to the volatile nature of JavaScript, we might want to check for the existence of key in an object like this −const obj = { name: 'Rahul' };if(!obj['fName']){}orif(obj['fName'] === undefined){}These both are incorrect ways. Why?Because in this case there happens to be no 'fName' key, but suppose there existed a 'fName' which was deliberately ... Read More
We are required to write a JavaScript function that takes in an array of numbers. Our function should return true if the difference between all adjacent elements is the same positive number, false otherwise.ExampleThe code for this will be −const arr = [4, 7, 10, 13, 16, 19, 22]; const growingMarginally = arr => { if(arr.length
const sort = ["this", "is", "my", "custom", "order"]; const myObjects = [ {"id":1, "content":"is"}, {"id":2, "content":"my"}, {"id":3, "content":"this"}, {"id":4, "content":"custom"}, {"id":5, "content":"order"} ];We are required to write a JavaScript function that takes in two such arrays and sorts the second array of objects on the basis of the first array so that the content property of objects are matched with the strings of the first array.Therefore, for the above arrays the output should look like −const output = [ {"id":3, "content":"this"}, {"id":1, "content":"is"}, {"id":2, "content":"my"}, {"id":4, "content":"custom"}, {"id":5, "content":"order"} ];ExampleThe ... Read More
We are required to write a JavaScript function that takes in an array of Numbers. The array may contain more than one greatest element (i.e., repeating greatest element).We are required to write a JavaScript function that takes in one such array and returns all the indices of the greatest element.ExampleThe code for this will be −const arr = [10, 5, 4, 10, 5, 10, 6]; const findGreatestIndices = arr => { const val = Math.max(...arr); const greatest = arr.reduce((indexes, element, index) => { if(element === val){ return indexes.concat([index]); } else { return indexes; }; }, []); return greatest; } console.log(findGreatestIndices(arr));OutputAnd the output in the console will be −[ 0, 3, 5 ]
We are required to write a JavaScript function that takes in an array of two numbers as the first argument, this array specifies a number range within which we can generate random numbers.The second argument will be a single number that specifies the count of random numbers we have to generate.Then at last our function should return the greatest all random numbers generated.ExampleThe code for this will be −const range = [15, 26]; const count = 10; const randomBetweenRange = ([min, max]) => { const random = Math.random() * (max - min) + min; return random; }; const ... Read More
We are required to write a JavaScript function that takes in a number, say num.Then our function should return the sum of all the natural numbers between 1 and num, including 1 and num.For example, if num is −const num = 5;Then the output should be −const output = 15;because, 1+2+3+4+5 = 15We will use the below formula to solve this problem −Sum of all natural number upto n =((n*(n+1))/2)ExampleThe code for this will be −const num = 5; const sumUpto = num => { const res = (num * (num + 1)) / 2; return res; }; ... Read More
We are required to write a JavaScript function that takes in an array of literals.Then the function should shuffle the order of elements in any random order inplace.ExampleThe code for this will be −const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; const unorderArray = arr => { let i, pos, temp; for (i = 0; i < 100; i++) { pos = Math.random() * arr.length | 0; temp = arr[pos]; arr.splice(pos, 1); arr.push(temp); }; } unorderArray(letters); console.log(letters);OutputAnd the output in the console will ... Read More
Suppose we have a complex JSON Object like this −const obj = { "id": "0001", "fieldName": "sample1", "fieldValue" "0001", "subList": [ { "id": 1001, "fieldName": "Sample Child 1", "fieldValue": "1001", "subList": [] }, { "id": 1002, "fieldName": "Sample Child 2", "fieldValue": "1002", "subList": [] } ] }We are required to write ... Read More