AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 410 of 840

Middle of three elements - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 426 Views

We are required to write a JavaScript function that takes in three unsorted numbers and returns the middlemost of them using minimum number of comparisons. For example: If the numbers are − 34, 45, 12 Then our function should return the following − 34 Algorithm Explanation The algorithm uses mathematical differences to determine the middle element without explicit sorting. By calculating differences between pairs and checking their signs, we can identify the middle value efficiently. Example Following is the code − const num1 = 34; const ...

Read More

How to read data from JSON array using JavaScript?

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

JSON arrays are a common data structure in JavaScript applications. This article demonstrates various methods to read and access data from JSON arrays. What is a JSON Array? A JSON array is a collection of JSON objects enclosed in square brackets. Each object can contain multiple key-value pairs. [ {"name": "Rohan", "age": 22}, {"name": "Shawn", "age": 12}, {"name": "Michael", "age": 21} ] Method 1: Using JSON.parse() and forEach() The most common approach is parsing the JSON string and iterating through the array: ...

Read More

What should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

Consider the following binary array (Array A) − const arr = [1, 0, 1, 1, 1, 1, 0, 1, 1]; When this array is passed through the function, say sumRight(), it produces the following output array (Array B) − const output = [1, 0, 4, 3, 2, 1, 0, 2, 1]; Understanding the Algorithm Elements in array arr can be either 0 or 1. The function counts backward from the last element of array arr, if there are consecutive 1's in the array arr then the corresponding element in the output ...

Read More

Wildcard matching of string JavaScript

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

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function should return true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, otherwise the function should return false. Let's write the code for this function — Example const str1 = 'first string'; const str2 = 'second string'; const wildcardMatching = (first, second, num) => { ...

Read More

How to remove the first character of link (anchor text) in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 464 Views

In JavaScript, you can remove the first character from link anchor text using the substring(1) method combined with DOM manipulation. This is useful when you need to programmatically fix incorrectly formatted link text. The Problem Sometimes links may have extra characters at the beginning that need to be removed. For example, "Aabout_us" should be "about_us" and "Hhome_page" should be "home_page". Using substring(1) Method The substring(1) method extracts characters from index 1 to the end, effectively removing the first character (index 0). ...

Read More

Maximum Possible Sum of Products in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 248 Views

We are given two arrays say, arr1 and arr2 of positive numbers. The number of values in both the arrays are the same. We are required to write a function that finds the maximum sum of products of their elements. Each element in arr1 has to be multiplied with exactly one element in arr2 and vice versa such that each element of both the arrays appears exactly once and the sum of product produced is maximum. Problem Understanding For example: if, arr1 = [5, 1, 3, 4, 2] and, arr2 = [8, 10, 9, 7, ...

Read More

How to add form validation for empty input fields with JavaScript?

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

Form validation ensures users provide required information before submitting. JavaScript allows client-side validation to check for empty input fields and provide immediate feedback. Basic Empty Field Validation Here's a complete example that validates empty input fields: Form Validation Example JavaScript Empty Input Field Validation Name: ...

Read More

Check if three consecutive elements in an array is identical in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 899 Views

We are required to write a JavaScript function, say checkThree() that takes in an array and returns true if anywhere in the array there exists three consecutive elements that are identical (i.e., have the same value) otherwise it returns false. Therefore, let's write the code for this function − Example const arr = ["g", "z", "z", "v" ,"b", "b", "b"]; const checkThree = arr => { const prev = { element: null, count: 0 ...

Read More

Separate odd and even in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 827 Views

In JavaScript, separating odd and even numbers means rearranging an array so that all even numbers appear before all odd numbers. This is commonly achieved using custom sorting functions or array methods like filter(). Using Custom Sort Function The most efficient approach uses Array.sort() with a custom comparator that prioritizes even numbers: const arr = [2, 6, 3, 7, 8, 3, 5, 4, 3, 6, 87, 23, 2, 23, 67, 4]; const isEven = num => num % 2 === 0; const sorter = (a, b) => { if(isEven(a) && !isEven(b)){ ...

Read More

Create increment decrement plus minus buttons programmatically for HTML input type number in JavaScript

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

In JavaScript, you can create increment and decrement buttons for HTML input type number using the stepUp() and stepDown() methods. This approach provides better user experience by allowing precise control over numeric inputs. On clicking Increment (+), the number in the input field increases by the step value On clicking Decrement (-), the number in the input field decreases by the step value The buttons respect the min/max constraints of the input field Basic Implementation Here's how to create increment/decrement buttons programmatically: ...

Read More
Showing 4091–4100 of 8,392 articles
« Prev 1 408 409 410 411 412 840 Next »
Advertisements