AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 485 of 840

Fat arrow functions in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 382 Views

Fat arrow functions, introduced in ES6, provide a shorter syntax for writing functions in JavaScript. They use the => operator instead of the function keyword. Syntax // Basic syntax (param1, param2, ...) => { } // Single parameter (parentheses optional) param => { } // No parameters () => { } // Single expression (return implicit) (a, b) => a + b Basic Example Fat Arrow Functions ...

Read More

Find the difference of largest and the smallest number in an array without sorting it in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 413 Views

We have an array of Numbers that are arranged in pure random order. Our job is to write a function that takes in one such array of Numbers and returns the difference of greatest and smallest numbers present in it, but without sorting the array. We will use the Array.prototype.reduce() function to pick the smallest and greatest numbers from the array and later will return their difference. Using Array.reduce() Method The reduce() method allows us to iterate through the array once and track both maximum and minimum values simultaneously: const arr = [23, 65, 67, ...

Read More

Get the last item from node list without using length property in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 509 Views

In JavaScript, you can get the last item from a NodeList without using the length property by leveraging CSS selectors, array methods, or destructuring. This is useful when you want to avoid calculating the length explicitly. Sample HTML Structure Let's use the following table as our example: John David Mike Method 1: Using CSS :last-child Selector The most direct approach is using CSS selectors to target the last element: ...

Read More

Using recursion to remove consecutive duplicate entries from an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 561 Views

We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space. For example, if the input array is − const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; Then the output should be − const output = [17, 12, 354, 1]; Recursive Solution The recursive approach modifies the array in-place by checking consecutive elements and removing duplicates: const arr = [17, 17, 17, ...

Read More

Select all elements with "data-" attribute with jQuery and display on Console?

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

To select all elements with "data-" attributes in jQuery, you can use the [data-*] selector or document.querySelectorAll(). This tutorial shows both approaches for selecting elements and displaying their data attribute values in the console. Method 1: Using jQuery Selector jQuery provides a simple way to select elements with data attributes using the [data-*] selector: Select Data Attributes with jQuery Paragraph with data attribute Heading ...

Read More

Assigning values to a computed property in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 239 Views

In JavaScript, computed properties allow you to create object properties with dynamic names. This article shows how to extract data from an array of objects and assign values to computed properties using array methods. Sample Data Let's start with an array of customer details objects: const customerDetails = [ {customerFirstName: "David"}, {customerLastName: "Miller"}, {customerCountryName: "US"}, {customerAge: "29"}, {isMarried: false}, {customerCollegeName: null} ]; console.log("Original data:", customerDetails); Original data: [ ...

Read More

Program to find largest of three numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 627 Views

We are required to write a JavaScript function that takes in three or more numbers and returns the largest of those numbers. For example: If the input numbers are − 4, 6, 7, 2, 3 Then the output should be − 7 Method 1: Using Math.max() with Spread Operator The simplest approach is using the built-in Math.max() function with the spread operator: const findLargest = (...nums) => { return Math.max(...nums); }; console.log(findLargest(4, 6, 7, 2, 3)); console.log(findLargest(15, 8, 23, 1)); ...

Read More

Alternative shuffle in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 400 Views

An alternatively shuffled array in JavaScript is an array of numbers where elements are arranged such that the greatest number is followed by the smallest element, second greatest element is followed by second smallest element, and so on. For example, if we have an input array: [11, 7, 9, 3, 5, 1, 13] The alternatively shuffled output should be: [13, 1, 11, 3, 9, 5, 7] How Alternative Shuffling Works The algorithm follows these steps: Sort the array in ascending order Take the largest element from the end ...

Read More

Return Top two elements from array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 375 Views

We have an array of numbers in JavaScript that contains numbers in an unsorted order. Our job is to write a function that takes in this array of numbers and returns an array of two elements, the top two elements of the array (greatest two elements of the array). We have to do this in one pass i.e., we need to execute this method in linear time like by using only one for loop or if we use ES6 function, we have to make sure to use only one and once and avoid nesting of methods which increases time ...

Read More

Create HTML Document with Custom URL for the document in JavaScript

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

In JavaScript, you can create an HTML document with a custom URL using document.implementation.createHTMLDocument() and the element. This technique allows you to set a base URL that affects how relative URLs are resolved within the document. How It Works The createHTMLDocument() method creates a new HTML document. By adding a element to the document's head, you establish a base URL that all relative URLs in the document will resolve against. Example Custom Base URL Example ...

Read More
Showing 4841–4850 of 8,392 articles
« Prev 1 483 484 485 486 487 840 Next »
Advertisements