AmitDiwan has Published 10740 Articles

Hyphen string to camelCase string in JavaScript

AmitDiwan

AmitDiwan

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

220 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

298 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

493 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

269 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

251 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

365 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

851 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

439 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

297 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

Compare the Triplets - JavaScript?

AmitDiwan

AmitDiwan

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

200 Views

For this, you need to use if condition to compare triplets.Let’s say we are passing the following values −35, 36, 37, 33, 48, 50ExampleFollowing is the code −function tripletsSolution(first, second, third, fourth, fifth, sixth) {    var storedResult = []    if (first > fourth || second > fifth || ... Read More

Advertisements