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 on Trending Technologies
Technical articles with clear explanations and examples
Find 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 MoreWhat is the shortest function of reading a cookie by name in JavaScript?
The shortest way to read a cookie by name in JavaScript is using a one-line function that searches through document.cookie and extracts the specific value. Shortest Cookie Reader Function Here's the most concise function to read a cookie by name: Cookie Reader // Set some cookies first for testing document.cookie = "username=john; path=/"; document.cookie = "theme=dark; path=/"; ...
Read MoreHow to set all the border right properties in one declaration with JavaScript?
To set all the border right properties in JavaScript, use the borderRight property. This property allows you to set the border color, style, and width at once using a single declaration. Syntax element.style.borderRight = "width style color"; Parameters The borderRight property accepts a string value with three components: width - Border thickness (e.g., "2px", "thick", "thin") style - Border style (e.g., "solid", "dashed", "dotted") color - Border color (e.g., "#000000", "red", "rgb(255, 0, 0)") Example You can try to run the following code to learn how to set all ...
Read MoreImprove performance of a HTML5 Canvas with particles bouncing around
To enhance the performance of HTML5 Canvas with particles bouncing around, several optimization techniques can dramatically improve frame rates and reduce CPU usage. Key Performance Optimization Techniques Separate the calculations from the drawing operations Request a redraw only after updating calculations Optimize collision detection by avoiding O(n²) comparisons Reduce callback usage and function calls Use inline calculations where possible Implement object pooling for particles Example: Optimized Particle System ...
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 More