Split One Dimensional Array into Two Dimensional Array in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:50:07

2K+ Views

We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (**if possible) and divide elements into them accordingly.** if the array contains 9 elements and we asked to make 4 subarrays, then dividing 2 elements in each subarray creates 5 subarrays and 3 in each creates 3, so in such cases we have to fallback to nearest lowest level (3 in this case) because our requirement is to distribute equal number of elements ... Read More

Check for Power of Two in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:42:28

2K+ Views

We are required to write a function, say isPowerOfTwo() that takes in a positive number and returns a boolean based on the fact whether or not the number is some power of 2.For example −console.log(isPowerOfTwo(3)); //false console.log(isPowerOfTwo(32)); //true console.log(isPowerOfTwo(2048)); //true console.log(isPowerOfTwo(256)); //true console.log(isPowerOfTwo(22)); //falseLet’s write the code for this function, it will be a very straightforward recursive function that keeps recurring until the number stays divisible by 2, if in this process the number gets reduced all the way down to 1, it is a power of 2 otherwise it isn’t. Here is the code −Exampleconst isPowerOfTwo = num => ... Read More

Write Function to Convert Array of Values to Object in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:41:04

577 Views

Let’s say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types.For example −// if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) {    'string' => [ 'class', 'name' ],    'number' => [ 2, 6 ],    'object' => [ [ 7, 8, 9 ], { name: 'Michael' } ],    'symbol' => [ Symbol(foo) ],    'boolean' => ... Read More

Recursively Flatten an Object in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:38:41

399 Views

We are required to write a function that does the following transformation −If the input object is −const input = {    a: 0,    b: {x: {y: 1, z: 2}},    c: 3 };Then the output of the function should be −const output = {    a: 0,    'b.x.y': 1,    'b.x.z': 2,    c: 3 }We basically have to write a function that flattens a nested object. Will do this via recursion and the code for doing this will be −Exampleconst obj = {    a: 1,    b: {x: {y: 1, z: 2}},    c: 3 ... Read More

Parse Array to Equal Intervals in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:36:25

221 Views

Let’s say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so that it gets divided into n equal intervals.For example −// if the input array is const arr = [12, 48]; // and the interval is 4 //then the output array should be: const output = [12, 21, 30, 39, 48];This way the array got divided into 4 equal intervals. So, ... Read More

Check If Every Property on Object is the Same Recursively in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:34:21

438 Views

Let’s say, we are required to write a function, say isSame() that accepts a nested object and returns a boolean depending on the fact whether or not all the keys have the same values. When saying all the keys we mean all the last keys like if a key has a nested object as its value, we are required to traverse to the end of the nested object and check for that value.For example − If the object is −const obj = {    a: 1,    b: 1,    c: {       aa: 1    } };Then ... Read More

Rearrange Array in Maximum Minimum Form by JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:31:44

274 Views

We are required to write a function, say minMax() that takes in an array of Numbers and rearranges the elements such that the greatest element appears first followed by the smallest elements then the second greatest element followed by second smallest element and so on.For example −// if the input array is: const input = [1, 2, 3, 4, 5, 6, 7] // then the output should be: const output = [7, 1, 6, 2, 5, 3, 4]So, let’s write the complete code for this function −Exampleconst input = [1, 2, 3, 4, 5, 6, 7]; const minMax = arr ... Read More

Count Elements in an Array Below or Above a Given Number in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:30:08

222 Views

Consider we have an array of Numbers that looks like this −const array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9];We are required to write a function that counts how many of the elements are in the array below / above a given number.For example, if the number is 5.25, the answer should be the following 5 elements, (3.1, 1, 2.2, 5.1, 2.1)and 3 elements above it −(6, 7.3, 9)Note − If any element is equal to the provided number, it should be counted as above the number.So, let’s write the code for this function −Exampleconst array = [3.1, ... Read More

Use PowerShell Help Commands

Chirag Nagrekar
Updated on 26-Aug-2020 08:27:28

5K+ Views

To get help for the particular command, you can use Get-Help (alias: help)  cmdlet with the command that you need help.For example, help Get-ServiceOnce you run this command, you will get the description of NAME, SYNOPSIS, SYNTAX, DESCRIPTION, RELATED LINKS, and REMARKS.Multiple parameters that support help as shown below-Full − Detailed help with parameter explanation and examples.help Get-Service -Full-Detailed − Detailed help of parameters and doesn’t include the examples.help Get-Service -Detailed-Examples − Only help related to examples will be displayed on the PowerShell screen.help Get-Service -Examples -Online − Help contents for the cmdlet will be searched online on the Microsoft ... Read More

Remove Windows Features Using PowerShell

Chirag Nagrekar
Updated on 26-Aug-2020 08:25:27

3K+ Views

To remove windows features, we can use command Remove-WindowsFeature command is used along with feature name.Remove-WindowsFeature Search-Service -VerboseVERBOSE: Uninstallation started... VERBOSE: Continue with removal? VERBOSE: Prerequisite processing started... VERBOSE: Prerequisite processing succeeded. Success Restart Needed Exit Code Feature Result ------- -------------- --------- -------------- True No Success {Windows Search Service} VERBOSE: Uninstallation succeeded.If the windows feature has the management tools like as in Web-Server (IIS) feature, you can add the same in the command line. If the server requires restart then you can add the -Restart parameter. For example, Remove-WindowsFeature Web-Server -IncludeManagementTools -Restart -VerboseIf we check the -Name parameter, it supports ... Read More

Advertisements