Found 6710 Articles for Javascript

Distance between 2 duplicate numbers in an array JavaScript

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, 3, 4, 2, 5, 4, 1, 3]; const findDistance = arr => {    var map = {}, res = {};    arr.forEach((el, ind) => {       map[el] = map[el] || [];       map[el].push(ind);    });    Object.keys(map).forEach(el => {       if (map[el].length > ... Read More

Hyphen string to camelCase string in JavaScript

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

192 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 this will be −const str = 'this-is-an-example'; const changeToCamel = str => {    let newStr = '';    newStr = str    .split('-')    .map((el, ind) => {       return ind && el.length ? el[0].toUpperCase() + el.substring(1)       : el;    })    .join('');    return newStr; }; console.log(changeToCamel(str));Following is the output on console −thisIsAnExample

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

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 code for this will be −const arr = [1, 2, 3, 4, 5, 6, 7]; const num = 14; const closestPair = (arr, sum) => {    let first = 0, second = 0;    for(let i in arr) {       for(let j in arr) {     ... Read More

Get max value per key in a JavaScript array

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

457 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 write a JavaScript function that takes in one such array and returns an array of objects.The array should contain an object for each unique value of "b" property where the "a" property has the highest value.The code for this will be −const arr = [    {a:1, b:"apples"},    {a:3, ... Read More

Finding transpose of a 2-D array JavaScript

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

242 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 res = [];    arr.forEach((el, ind) => {       el.forEach((elm, index) => {          res[index] = res[index] || [];          res[index][ind] = elm;       });    });    return res; }; console.log(transpose(arr));Method 2: Using Array.prototype.reduce()const arr = [   ... Read More

Hexadecimal color to RGB color JavaScript

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

221 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'; const hexToRGB = hex => {    let r = 0, g = 0, b = 0;    // handling 3 digit hex    if(hex.length == 4){       r = "0x" + hex[1] + hex[1];       g = "0x" + hex[2] + hex[2];       ... Read More

RGB color to hexadecimal color JavaScript

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

330 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 rgbColor = {    red: 0,    green: 51,    blue: 155 } function rgbToHex({    red: r,    green: g,    blue: b }) {    const prefix = '#';    const hex = prefix + ((1

How to replace before first forward slash - JavaScript?

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

829 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 program, you need to use the following command −node fileName.js. Here, my file name is demo245.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo245.js Original value=welcome/name/john/age/32 After replacing the value=index/name/john/age/32Read More

Call a function with onclick() – JavaScript?

Disha Verma
Updated on 07-Mar-2025 12:56:12

4K+ Views

The onclick event is a useful feature for calling a function in JavaScript. The onclick event allows you to execute a function when a user interacts with an element, such as clicking a button. This article will guide you on how to use the onclick event to call a JavaScript function. What is Onclick Event? The onclick event is triggered when a user clicks on an HTML element. It is commonly used to execute a JavaScript function or a block of code in response to a mouse click on an HTML element, such as a button, link, or any ... Read More

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

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 of space.ExampleFollowing is the code −const employeeDetails = [    {       employeeName: "John Smith",       employeeTechnology: "JavaScript HTML"    },    {       employeeName: "David Miller",       employeeTechnology: "Java Angular"    } ] const objectValues = employeeDetails.map(emp => {    var ... Read More

Advertisements