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 431 of 840
How to find elements of JavaScript array by multiple values?
In JavaScript, you can find elements of an array by multiple values using various methods. The most common approaches are using filter() with includes() or checking if an array contains all elements from another array. Method 1: Using filter() with includes() This method filters an array to find elements that match any of the specified values: Find Elements by Multiple Values Find Array Elements by Multiple Values ...
Read MoreGet unique item from two different array in JavaScript
We are required to make a function that accepts an array of arrays, and returns a new array with all elements present in the original array of arrays but remove the duplicate items. For example − If the input is − const arr = [ [12, 45, 65, 76, 76, 87, 98], [54, 65, 98, 23, 78, 9, 1, 3], [87, 98, 3, 2, 123, 877, 22, 5, 23, 67] ]; Then the output should be single array of unique elements like this − ...
Read MoreHow to inherit CSS properties of parent Element using JavaScript?
JavaScript provides several ways to make child elements inherit CSS properties from their parent elements. You can use classList.add() to apply CSS classes, or directly manipulate the style property to inherit specific styles. Using classList.add() Method The most common approach is creating elements and adding CSS classes that define inheritance rules. CSS Inheritance Example .parent-container { ...
Read MoreFind the primorial of numbers - JavaScript
The primorial of a number n is equal to the product of the first n prime numbers. This is different from factorial, which multiplies consecutive integers. For example, if n = 4, we need the first 4 prime numbers: 2, 3, 5, and 7. 2 × 3 × 5 × 7 = 210 Let's write a JavaScript function that calculates the primorial of a given number. Understanding Prime Numbers First, we need a helper function to check if a number is prime: const isPrime = n => { ...
Read MoreHow to stop a function during its execution in JavaScript?
In JavaScript, you can stop a function during execution using different techniques depending on the function type. For repeating functions like setInterval(), use clearInterval(). For one-time delayed functions, use clearTimeout(). Stopping setInterval() Functions The most common scenario is stopping a repeating function created with setInterval(): Stop Function Execution Start Function Stop Function ...
Read MoreReplacing array of object property name in JavaScript
In JavaScript, you can replace property names in an array of objects using the map() method to create new objects with renamed properties. This technique is useful when you need to transform data structures or adapt objects to different naming conventions. Syntax const newArray = originalArray.map(item => ({ newPropertyName: item.oldPropertyName, // ... other properties })); Example: Renaming Object Properties Replace Object Property Names ...
Read MoreValidate input: replace all 'a' with '@' and 'i' with '!'JavaScript
We need to write a function validate() that takes a string as input and returns a new string where all occurrences of 'a' are replaced with '@' and all occurrences of 'i' are replaced with '!'. This is a classic string manipulation problem that can be solved using different approaches. Let's explore the most common methods. Using For Loop The traditional approach iterates through each character and builds a new string: const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => { let validatedString = ''; ...
Read MoreLose input hints (get focus) after pressing mouse cursor in a textbox with JavaScript
In this tutorial, we'll learn how to clear placeholder-like text from an input field when the user clicks on it (gets focus) and restore it when the field loses focus if no text was entered. Let's say we have the following input field with hint text: StudentName: To lose input hints on pressing mouse cursor, we use the onfocus and onblur events. How It Works The solution uses two JavaScript functions: onfocus: Clears the hint text when user clicks in the field onblur: Restores hint ...
Read MoreRange Overflow and Range Underflow properties in JavaScript.
In JavaScript, the rangeOverflow and rangeUnderflow properties are part of the ValidityState object, used to check if form input values exceed their specified ranges. Range Underflow The rangeUnderflow property returns true if the element's value is less than the value specified in the min attribute. Range Overflow The rangeOverflow property returns true if the element's value is greater than the value specified in the max attribute. Syntax element.validity.rangeOverflow // true if value > max element.validity.rangeUnderflow // true if value < min Example ...
Read MoreHow to merge two JavaScript objects?
Merging two JavaScript objects combines their properties into a single object. There are several methods to achieve this, with the spread operator being the most modern and widely used approach. Using the Spread Operator (Recommended) The spread operator (...) is the cleanest and most readable way to merge objects: Merge JavaScript Objects Merge Two JavaScript Objects MERGE OBJECTS ...
Read More