AmitDiwan has Published 10744 Articles

Distance between 2 duplicate numbers in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:18:54

172 Views

We are required to write a JavaScript function that takes in an array of numbers that contains at least one duplicate pair of numbers.Our function should return the distance between all the duplicate pairs of numbers that exist in the array.The code for this will be −const arr = [2, ... Read More

Hyphen string to camelCase string in JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:16:54

191 Views

Suppose, we have a string that contains words separated by hyphens like this −const str = 'this-is-an-example';We are required to write a JavaScript function that takes in one such string and converts it into a camelCase string.For the above string, the output should be −const output = 'thisIsAnExample';The code for ... Read More

Finding closest pair sum of numbers to a given number in JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:14:20

280 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a Number as the second argument.The function should return an array of two numbers from the original array whose sum is closest to the number provided as the second argument.The ... Read More

Get max value per key in a JavaScript array

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:12:28

456 Views

Suppose, we have an array of objects like this −const arr = [    {a:1, b:"apples"},    {a:3, b:"apples"},    {a:4, b:"apples"},    {a:1, b:"bananas"},    {a:3, b:"bananas"},    {a:5, b:"bananas"},    {a:6, b:"bananas"},    {a:3, b:"oranges"},    {a:5, b:"oranges"},    {a:6, b:"oranges"},    {a:10, b:"oranges"} ];We are required to ... Read More

Finding transpose of a 2-D array JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:10:00

241 Views

We are required to write a JavaScript function that takes in a two-dimensional array and returns its transposed array.The code for this will be −Method 1: Using Array.prototype.forEach()const arr = [    [0, 1],    [2, 3],    [4, 5] ]; const transpose = arr => {    const ... Read More

Hexadecimal color to RGB color JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:08:01

220 Views

We are required to write a JavaScript function that takes in a hexadecimal color and returns its RGB representation.The function should return an object containing the respective values of red green and blue color −For example:hexToRgb('#0080C0') should return 0, 128, 192The code for this will be −const hex = '#0080C0'; ... Read More

RGB color to hexadecimal color JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:05:17

329 Views

We are required to write a JavaScript function that takes in a RGB color and returns its hexadecimal representation.The function should take in an object containing three numbers representing the respective values of red green and blue color.For example:rgbToHex(0, 128, 192) should return '#0080C0'The code for this will be −const ... Read More

How to replace before first forward slash - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:38:48

828 Views

Let’s say the following is our string with forward slash −var queryStringValue = "welcome/name/john/age/32"To replace before first forward slash, use replace() along with regular expressions.ExampleFollowing is the code −var regularExpression = /^[^/]+/ var queryStringValue = "welcome/name/john/age/32" var replacedValue = queryStringValue.replace(regularExpression, 'index'); console.log("Original value="+queryStringValue); console.log("After replacing the value="+replacedValue);To run the above ... Read More

How to get only first word of object's value – JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:34:13

425 Views

Let’s say the following is our object −const employeeDetails = [    {       employeeName: "John Smith",       employeeTechnology: "JavaScript HTML"    },    {       employeeName: "David Miller",       employeeTechnology: "Java Angular"    } ]You can use split() on the basis ... Read More

How to redundantly remove duplicate elements within an array – JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:31:39

269 Views

Let’s say the following are our array elements −10, 20, 10, 50, 60, 10, 20, 40, 50To remove duplicate elements, use … new Set().ExampleFollowing is the code −var arrayWithNoDuplicateNumbers = [...new Set([10, 20, 10, 50, 60, 10, 20, 40, 50])]; console.log("No Duplicate values=") console.log(arrayWithNoDuplicateNumbers);To run the above program, use the ... Read More

Advertisements