Splatting is the way to pass the collection of the parameters to the command as a single value. It uses the Hashtable splatting means that we can pass the Name and Value pair combination. We can use the named positional parameter for this with the values we want to provide.For example, First, we will check how we run the Copy-Item command here without splatting, $params = @{ Path = 'C:\Temp\25Aug2020.txt' Destination = 'C:\test1' Verbose = $true Force = $true } Copy-Item @paramsAnother Example, $hash = @{ From = 'harris@Microsoftmail.com' To = 'Jacob@MicrosoftMail.com' SMTP ... Read More
PowerShell splatting is a method to pass the collection of the parameters as a single command unit which makes the command shorter and easier for the user to read commands. Splatting uses the symbol (@) instead of ($) which tells the user that splatting is used and PowerShell is passing a set of values instead of a single value.Splatting in PowerShell was included from the v3.0 onwards and you can pass all parameters in the command.For example, $params = @{ Path = 'C:\Temp\25Aug2020.txt' Destination = 'C:\test1' Verbose = $true Force = $true } Copy-Item @paramsThe splatting ... Read More
We are required to write a JavaScript function that takes in an array of positive integers as the first argument and a positive integer as the second argument.The function should figure out and return the length of the smallest subarray that we should delete from the original array in order to make its sum divisible by the number specified by the second argument.For example −If the input is −const arr = [3, 8, 2, 6]; const num = 9;Then the output should be −const output = 2Because the subarray that needs to be deleted is [8, 2]ExampleFollowing is the code ... Read More
We are required to write a JavaScript function that takes in an array of numbers. The array given as an input to the function have two special properties −The length of the array will always be an even number.The number of even numbers and the number of odd numbers in the array will always be equal (i.e., both being equal to the half of the length of array)The function should shuffle the elements of the array such that all the even values occupy even indices and all the odd values occupy odd indices.Note that there may be more than one ... Read More
We are required to write a JavaScript function that takes in a character string as the first and the only argument.The function should try and re-organize the characters present in the string such that no two identical characters are placed adjacent to each other.If there exists at least one such combination then our function should return that combination string otherwise our function should return an empty string.For example −If the input string is −const str = 'add';Then our function can output −const output = 'dad';ExampleFollowing is the code −const str = 'add'; const formatString = (str = '') => { ... Read More
We are required to write a JavaScript function that takes in two arrays of numbers, lets call them arr1 and arr2.The function should build a third array based on the two input arrays that contains all the elements that are common to both arr1 and arr2.Note that if there are more than one same element that are present in both the arrays then we have to consider all such instances of that element.For example −If the input arrays are −const arr1 = [1, 2, 2, 4, 4, 5, 6]; const arr2 = [3, 2, 4, 2, 4, 9];Then the output ... 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 job of our function is to sort the elements present in the array in an alternate fashion.By alternate we mean the following −Suppose we have an array arr that contains only four elements for the time being. Then our function should shuffle the elements of the array such that −arr[0] < arr[1] > arr[2] < arr[3]Note that there can be more than one possible solution for a given array, we are just required to return any of the ... Read More
Suppose we have an array of positive integers that represents the number of citations a particular researcher has conducted over a period of time.We are required to write a JavaScript function that takes in one such array and the function should find the h-index of that researcher based on the citations data represented by the array.H-Index:Consider a researcher who performed N number of citations in his career. Then the researcher has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.For example ... Read More
We are required to write a JavaScript function that takes in three arguments, namely −arr --> an array of integers m --> a positive integer n --> a positive integerThe task of our function is to find out whether there exists two such elements (lets call them a1 and a2) such that −The absolute difference between a1 and a2 is at most mThe absolute difference between the indices of a1 and a2 is at most nExampleFollowing is the code −const arr = [1, 2, 3, 1, 7, 8]; const findSpecialElements = (arr = [], m, n) => { const ... 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 string together the numbers present in the array such that form the greatest possible number that can be formed from those given set of numbers.For example −If the input array is −const arr = [5, 45, 34, 9, 3];Then the output should be −const output = '9545343';ExampleFollowing is the code −const arr = [5, 45, 34, 9, 3]; const largestNumber = (arr = []) => { if(arr.every( n => n === 0)){ ... Read More