AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 363 of 840

How to generate array key using array index – JavaScript associative array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 389 Views

In JavaScript, you can create associative arrays (objects) using array indices as keys. This is useful for mapping array elements to their positions or creating key-value pairs from existing arrays. Using forEach() Method The forEach() method provides access to both the element and its index, allowing you to create dynamic object properties: var result = {}; var names = ['John', 'David', 'Mike', 'Sam', 'Bob', 'Adam']; names.forEach((nameObject, counter) => { var generatedValues = { [nameObject]: counter }; Object.assign(result, generatedValues); }); console.log(result); { John: 0, ...

Read More

Retrieve property value selectively from array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 368 Views

When working with arrays of objects in JavaScript, you often need to extract specific property values based on certain conditions. This tutorial shows how to retrieve property values selectively from an array of objects. Suppose we have an array of objects like this: const arr = [ { id : "23", name : "Item 1", isActive : true}, { id : "25", name : "Item 2", isActive : false}, { id : "26", name : "Item 3", isActive : false}, { id : "30", name ...

Read More

Distance of nearest 0 in binary matrix in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 290 Views

A binary matrix is an array of arrays containing only 0 or 1. We are required to write a JavaScript function that takes in a binary matrix as the only argument. Our function should create a new matrix containing the same number of rows and columns, and for each element of the original matrix the resulting matrix should contain that element's nearest distance from 0 in the original matrix. We have to keep in mind that while calculating distance it can move either horizontally or vertically and not diagonally. And it's guaranteed that the matrix contains at least ...

Read More

Constructing an array of addition/subtractions relative to first array element in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 201 Views

We are required to write a JavaScript function that takes in an array of positive integers. Our function should map this array to an array of string integers. The array should contain the number we should add/subtract to the first element to achieve the corresponding element. Problem For example, if we have: [4, 3, 6, 2] We need to calculate the difference between each element and the first element (4): 4 - 4 = 0 → "+0" 3 - 4 = -1 → "-1" 6 - 4 = 2 → "+2" 2 ...

Read More

Add time to string data/time - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

In JavaScript, you can add time to a date/time string using the Date object's built-in methods. This is useful for calculating future times or adjusting existing timestamps. Basic Approach First, create a Date object from your string, then use methods like setHours(), setMinutes(), or setSeconds() combined with their getter counterparts to add time. var dateValue = new Date("2021-01-12 10:10:20"); dateValue.setHours(dateValue.getHours() + 2); // Add 2 hours Example: Adding Hours to Date String var dateValue = new Date("2021-01-12 10:10:20"); console.log("Original date: " + dateValue.toString()); // Add 2 hours dateValue.setHours(dateValue.getHours() + 2); ...

Read More

Calculate the hypotenuse of a right triangle in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 797 Views

We are required to write a JavaScript function that takes in two numbers. The first number represents the length of the base of a right triangle and the second is perpendicular. The function should then compute the length of the hypotenuse based on these values. For example − If base = 8, perpendicular = 6 Then the output should be 10 The Pythagorean Theorem The hypotenuse is calculated using the Pythagorean theorem: c² = a² + b², where c is the hypotenuse and a, b are the other two sides. ...

Read More

Reversing strings with a twist in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

We are required to write a JavaScript function that takes in a string str as the first argument and an integer num as the second argument. Our function should reverse the first num characters for every 2 * num characters counting from the start of the string. If there are less than num characters left, we have to reverse all of them. If there are less than 2 * num but greater than or equal to num characters, then we have to reverse the first num characters and leave the others as original. Example Input and Output ...

Read More

Longest string consisting of n consecutive strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 455 Views

Problem We are required to write a JavaScript function that takes in an array of strings. Our function should create combinations by combining all possible n consecutive strings in the array and return the longest such string that comes first. Example Following is the code − const arr = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; const num = 2; function longestConsec(strarr, k) { if (strarr.length == 0 || k > strarr.length || k longStr.length) { ...

Read More

Generating desired pairs within a range using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 292 Views

We are required to write a JavaScript function that takes in a number n. Our function should generate an array containing the pairs of integers [a, b] that satisfy the following conditions: 0

Read More

JavaScript Algorithm - Removing Negatives from the Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), we need to write a function that removes any negative values from the array using only the pop() method without creating temporary arrays. The challenge is to modify the original array in-place, maintaining only positive numbers while using just the pop() method for removal. Algorithm Approach The algorithm works in two phases: Remove all negative values from the end of the array For remaining negatives, replace them with the last positive element and pop Complete Implementation function ...

Read More
Showing 3621–3630 of 8,392 articles
« Prev 1 361 362 363 364 365 840 Next »
Advertisements