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
Web Development Articles
Page 203 of 801
How to replace elements in array with elements of another array in JavaScript?
In JavaScript, there are several ways to replace elements in one array with elements from another array. The most common approach uses the splice() method to modify the original array in place. Input-Output Scenarios Let's look at some common scenarios for replacing array elements: Array1 = [1, 3, 5, 7, 2, 4]; Array2 = [3, 6]; // Replace first 2 elements of Array1 with Array2 Output = [3, 6, 5, 7, 2, 4] Array1 = [3, 6]; Array2 = [1, 3, 5, 7, 2, 4]; // Replace all elements of Array1 with Array2 ...
Read MoreRecursion example in JavaScript to display numbers is descending order?
In this article, we will display numbers in descending order using recursion in JavaScript. Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. Input-Output Scenario Let's look at an input-output scenario where we need to print numbers in descending order from a given number to 1: Input = 10 Output = 10 9 8 7 6 5 4 3 2 1 What is Recursion? The process of a function calling itself is called recursion. The function which calls itself is known as a recursive ...
Read MoreHow to get all combinations of some arrays in JavaScript?
Getting all combinations of arrays in JavaScript can be accomplished using recursive generator functions or iterative approaches. This is useful for creating permutations, product combinations, and other algorithmic solutions. Using Generator Function (Cartesian Product) A generator function can efficiently create all combinations by recursively building each possibility: function getAllCombinations(values) { function* generateCombinations(size, current) { if (size) { for (var element of values) { ...
Read MoreConstructing full name from first name, last name and optional middle name in JavaScript
We need to write a JavaScript function that takes in three strings: first name, last name, and an optional middle name, then returns the full name constructed from these inputs. Problem The challenge is handling the optional middle name parameter. If no middle name is provided, we should construct the full name with just the first and last names, avoiding extra spaces. Using Array Filter Method This approach uses an array to collect all name parts, filters out empty values, and joins them with spaces: const firstName = 'Vijay'; const lastName = 'Raj'; ...
Read MoreRemoving listener from inside outer function in JavaScript?
When working with event listeners in JavaScript, you might need to remove a listener from within an outer function. This is accomplished using removeEventListener() by passing the element reference and function reference to the outer function. How It Works The key is to pass both the element (this) and the function reference to the outer function, then use removeEventListener() to detach the listener. Example Remove Event Listener Example Press Me ...
Read MoreRealtime moving average of an array of numbers in JavaScript
A moving average calculates the average of elements from the start of an array up to each position. For each element at index i, we compute the average of elements from index 0 to i. Problem We need to write a JavaScript function that takes an array of numbers and returns a new array containing the cumulative moving average at each position. Input: [1, 2, 3, 4, 5] Output: [1, 1.5, 2, 2.5, 3] The first element is the average of just the first element (1/1 = 1). The second element is the average ...
Read MoreGet price value from span tag and append it inside a div after multiplying with a number in JavaScript?
In JavaScript, you can extract a numeric value from a span element, multiply it, and display the result in another element. This involves getting the text content, converting it to a number, performing the calculation, and updating the DOM. HTML Structure First, let's create a proper HTML structure with a span containing the price and a div to display the result: Price Calculator Original ...
Read MoreMatch specific word in regex in JavaScript?
In JavaScript, matching specific words using regex (regular expressions) is a common task for string pattern matching. This article covers the main methods to match words: test(), match(), and matchAll(). Understanding Word Boundaries The \b assertion represents a word boundary, ensuring you match complete words rather than partial matches within other words. Word Boundary Example const sentence = "mickey is holding mic."; const regex = /\bmic\b/; ...
Read MoreWebDriver click() vs JavaScript click().
In Selenium WebDriver, there are two primary methods to click elements: the standard WebDriver click() method and JavaScript click() through JavaScriptExecutor. Each approach has distinct advantages and use cases. WebDriver click() Method The standard WebDriver click() simulates actual user interaction. It waits for the element to be visible and clickable before performing the action. This method works with various locators including link text and partial link text. WebDriver click() Browser Engine ...
Read MoreWhy addEventListener to 'select' element does not work in JavaScript?
When working with HTML elements in JavaScript, developers often encounter issues with event listeners not working as expected. The key is understanding which events work with select elements and how to properly attach them using querySelector() and addEventListener(). Understanding the Change Event The element fires a change event when the user selects a different option. Unlike input events that fire continuously, the change event only fires when the selection is complete. Required Methods The addEventListener() Method - Registers an event handler for a specified event type on an element. target.addEventListener(event, function, useCapture) ...
Read More