Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 485 of 840
Fat arrow functions in JavaScript
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 MoreFind the difference of largest and the smallest number in an array without sorting it in JavaScript
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 MoreGet the last item from node list without using length property in JavaScript?
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 MoreUsing recursion to remove consecutive duplicate entries from an array - JavaScript
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 MoreSelect all elements with "data-" attribute with jQuery and display on Console?
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 MoreAssigning values to a computed property in JavaScript?
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 MoreProgram to find largest of three numbers - JavaScript
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 MoreAlternative shuffle in JavaScript
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 MoreReturn Top two elements from array JavaScript
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 MoreCreate HTML Document with Custom URL for the document in JavaScript
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