Suppose, we have an array of objects like this −const arr = [ { "customer": "Customer 1", "project": "1" }, { "customer": "Customer 2", "project": "2" }, { "customer": "Customer 2", "project": "3" } ]We are required to write a JavaScript function that takes one such array, and yields (returns) a new array.In the new array, all the customer keys with same values should be merged and the output should look something like this −const output ... Read More
We are required to write a JavaScript function that takes in an array of strings, (they may be a single character or greater than that). Our function should simply count all the vowels contained in the array.ExampleLet us write the code −const arr = ['Amy','Dolly','Jason','Madison','Patricia']; const countVowels = (arr = []) => { const legend = 'aeiou'; const isVowel = c => legend.includes(c); let count = 0; arr.forEach(el => { for(let i = 0; i < el.length; i++){ if(isVowel(el[i])){ count++; }; }; }); return count; }; console.log(countVowels(arr));OutputAnd the output in the console will be −10
Suppose we have an array that contains name of some people like this:const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia'];We are required to write a JavaScript function that takes in one such string as the first argument, and two lowercase alphabet characters as second and third argument. Then, our function should filter the array to contain only those elements that start with the alphabets that fall within the range specified by the second and third argument.Therefore, if the second and third argument are 'a' and 'j' respectively, then the output should be −const output = ['Amy', 'Dolly', 'Jason'];ExampleLet us write ... Read More
We have two sets of ranges; one is a single range of any length (R1) and the other is a set of ranges (R2) some or parts of which may or may not lie within the single range (R1).We need to calculate the sum of the ranges in (R2) - whole or partial - that lie within the single range (R1).const R1 = [20, 40]; const R2 = [[14, 22], [24, 27], [31, 35], [38, 56]];Result = 2+3+4+2 = 11R1 = [120, 356]; R2 = [[234, 567]];Result 122ExampleLet us write the code −const R1 = [20, 40]; const R2 = [[14, 22], ... Read More
We are required to write a JavaScript function that takes in an array of literals and a number and splits the array (first argument) into groups each of length n (second argument) and returns the two-dimensional array thus formed.If the array and number is −const arr = ['a', 'b', 'c', 'd']; const n = 2;Then the output should be −const output = [['a', 'b'], ['c', 'd']];ExampleLet us now write the code −const arr = ['a', 'b', 'c', 'd']; const n = 2; const chunk = (arr, size) => { const res = []; for(let i = 0; i ... Read More
To set the line breakpoint in the script, we can use the Set-PSBreakpoint command with -Line parameter and need to give the path of the script on which the Line breakpoint needs to set.Consider we have the script below which retrieves the value up to 99, starting from 1, We will set the Line Breakpoint at line number 3 so we will use the below command. Here the script name is WhieScript.ps1 and stored at C:\temp.Set-PSBreakpoint C:\temp\WhileScript.ps1 -Line 3Once you run the above command, you will get the output with the details as shown below.ID Script Line Command Variable ... Read More
Breakpoint in the PowerShell is the part of the debugger in the PowerShell commands. We use the breakpoints in PowerShell mainly for troubleshooting and logging purpose.There are three ways to set the breakpoint in PowerShell.Line BreakPoint (Can set the breakpoint for single or multiple lines)Command BreakPoint (Can set the breakpoint for commands or functions)Variable Breakpoint (Can set the breakpoint on a variable or multiple variables).We can’t set the breakpoint for the remote computer. To set the breakpoint for the remote computer, we first need to copy the script to the remote computer and then need to set the breakpoint as ... Read More
To check if the file is empty using PowerShell, we can use the string method called IsNullorWhiteSpace(). This method provides result true if the file is empty or only contains the white spaces otherwise false.For example, We have a test2.txt text file which has whitespaces.Example[String]::IsNullOrWhiteSpace((Get-content C:\Test2.txt))OutputTrueBut if you have a file like CSV which contains few headers but the data is empty, in that case, Get-Content will show the wrong output because it will consider headers. For example, Example[String]::IsNullOrWhiteSpace((Get-content C:\Temp\NewUsers.csv))OutputFalse Because the file has headers.PS C:\> Get-Content C:\Temp\NewUsers.csv Name, FirstName, Surname, EMPNumber, CountryIn that case, we can use the Import-CSV ... Read More
To open any file using its default application, we can use the Invoke-Expression command. For example, we want to open a file on the C:\temp and the file name is NewUsers.CSV then you can run the below command.Invoke-Expression C:\Temp\NewUsers.csvThe above command will open the file from that location. If the default application is not set then Windows will ask for the default application to select.If you know any application name and which can be opened with the shortcut then you can directly type the name of the application. For example, Notepad.exe, Calc.exeGenerally, they can be opened directly but this command ... Read More
We have a raw text example and to convert it into the CSV values, we can use below code.ExamplePS C:\> $text = "This is a PowerShell latest version" PS C:\> $text.Replace(' ', ', ')OutputThis, is, a, PowerShell, latest, versionIf there are multiple spaces between the keywords then the above replace command would go wrong. For example, ExamplePS C:\> $text = "This is a PowerShell latest version" PS C:\> $text.Replace(' ', ', ')OutputThis, , is, , , a, , , , PowerShell, latest, version So we can use another method as shown below.$text -replace '\s+', ' 'In the above command, \S ... Read More