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
Highest and lowest in an array JavaScript
In JavaScript, finding the difference between the highest and lowest values in an array is a common task. This article demonstrates multiple approaches to calculate this difference efficiently. Using Math.max() and Math.min() with Spread Operator The most straightforward approach uses the spread operator with Math.max() and Math.min(): const arr = [23, 54, 65, 76, 87, 87, 431, -6, 22, 4, -454]; const arrayDifference = (arr) => { const max = Math.max(...arr); const min = Math.min(...arr); return max - min; }; console.log("Array:", arr); ...
Read MoreWrite an algorithm that takes an array and moves all of the zeros to the end JavaScript
We need to write a function that moves all zeros in an array to the end while maintaining the order of non-zero elements. This algorithm should work in-place without using extra space. Problem Overview Given an array with mixed numbers including zeros, we want to push all zeros to the end while keeping other elements in their relative order. For example, [1, 0, 3, 0, 5] should become [1, 3, 5, 0, 0]. Method 1: Using splice() and push() This approach removes zeros when found and adds them to the end: const arr = ...
Read MoreHow can I declare and define multiple variables in one statement with JavaScript?
In JavaScript, you can declare and define multiple variables in a single statement by separating them with commas. This approach works with var, let, and const keywords. Syntax var variable1 = value1, variable2 = value2, variable3 = value3; // Or with let/const let variable1 = value1, variable2 = value2, variable3 = value3; Example: Using var var firstName = "My First Name is David", lastName = "My Last Name is Miller", ...
Read MoreHow to work with document.forms in JavaScript?
In this tutorial, let us discuss how to work with document.forms in JavaScript. The document.forms property returns a collection of all form elements in the document. This property is read-only and provides access to form elements, their properties, and methods. It's part of the DOM Level 1 specification. Syntax var forms = document.forms; let formLen = forms.length; let formId = forms[0].id || forms.item(0).id; let formItemId = forms[0].elements[0].id; let formItemVal = forms[0].elements[0].value; let formData = forms.namedItem("testForm").innerHTML; The above syntax returns the collection of forms, total number of forms, form ID, form element ID, form element ...
Read MoreHow to fire after pressing ENTER in text input with HTML?
There are several ways to detect when the ENTER key is pressed in a text input field. Here are the most common and effective approaches. Using jQuery with Custom Event This approach creates a custom "enterKey" event that triggers when ENTER is pressed: $(document).ready(function(){ $('input').bind("enterKey", function(e){ ...
Read MoreAdd elements to a linked list using Javascript
In JavaScript, adding elements to a linked list requires careful pointer manipulation to maintain the list structure. We need to create a function insert(data, position) that inserts data at the specified position. Implementation Steps Create a new Node with the provided data Check if the list is empty. If so, add the node as head and return Iterate to the desired position using a current element pointer Update the new node's next pointer to point to the current node's next Update the current node's next pointer to point to the new node Visual Representation ...
Read MoreDot notation vs Bracket notation in JavaScript
The dot notation and bracket notation are both methods for accessing object properties in JavaScript. While dot notation is cleaner and more commonly used, bracket notation provides greater flexibility, especially when working with dynamic property names or property names that contain special characters. Syntax Here's the basic syntax for both notations: // Dot notation object.propertyName // Bracket notation object["propertyName"] object[variableName] Basic Example: Accessing Properties Dot vs Bracket Notation ...
Read MoreFind duplicate element in a progression of first n terms JavaScript
When you have an array of first n natural numbers with one duplicate element, finding the duplicate efficiently is a common programming challenge. We'll explore two mathematical approaches that solve this in linear time. Method 1: Using Array.prototype.reduce() This approach uses a mathematical trick with the reduce function to find the duplicate in a single pass: const arr = [2, 3, 1, 2]; const duplicate = a => a.reduce((acc, val, ind) => val + acc - (ind + 1)) + a.length - 1; console.log(duplicate(arr)); 2 How the Reduce Method Works ...
Read MoreLoop through array and edit string JavaScript
Let's say, we have to write a function, say translate() that accepts a string as the first argument and any number of words after that. The string will actually contain n $ signs like this − This $0 is more $1 just a $2. Then there will be 3 strings which will replace the corresponding places. For example − If the function call is like this: translate('This $0 is more $1 just a $2.', 'game', 'than', 'game'); The output of the function should be: This game is more ...
Read MoreConcatenate two arrays of objects and remove repeated data from an attribute in JavaScript?
When concatenating two arrays of objects, you often need to remove duplicates based on a specific attribute. This article demonstrates how to merge arrays while handling duplicate entries using map() and find() methods. The Challenge Consider two arrays of product objects where some products appear in both arrays with different properties. We want to concatenate them but prioritize data from the second array when duplicates exist. Example: Merging Product Arrays Let's merge two product arrays and remove duplicates based on productId: var details1 = [ { ...
Read More