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 477 of 840
How to get only the first n% of an array in JavaScript?
We are required to write a function that takes in an array arr and a number n between 0 and 100 (both inclusive) and returns the n% part of the array. Like if the second argument is 0, we should expect an empty array, complete array if it's 100, half if 50, like that. And if the second argument is not provided it should default to 50. Therefore, the code for this will be − Syntax const byPercent = (arr, n = 50) => { const requiredLength = Math.floor((arr.length * n) / ...
Read MoreHow to load a JavaScript file Dynamically?
Loading JavaScript files dynamically allows you to add scripts to your webpage at runtime, which is useful for conditional loading, lazy loading, or modular applications. Basic Dynamic Loading Method The most common approach is creating a element and appending it to the document head: Dynamic JavaScript Loading body { font-family: "Segoe UI", Tahoma, ...
Read MoreAwaiting on dynamic imports in JavaScript.
Dynamic imports in JavaScript allow you to load modules asynchronously at runtime using the import() function. This is particularly useful for code splitting and loading modules only when needed. Syntax const module = await import('./modulePath.js'); // or import('./modulePath.js').then(module => { // use module }); How Dynamic Imports Work The import() function returns a Promise that resolves to the module object. You can use await to handle it asynchronously: Dynamic Imports Example ...
Read MoreFind average of each array within an array JavaScript
We are required to write a function getAverage() that accepts an array of arrays of numbers and we are required to return a new array of numbers that contains the average of corresponding subarrays. Let's write the code for this. We will map over the original array, reducing the subarray to their averages like this − Example const arr = [[1, 54, 65, 432, 7, 43, 43, 54], [2, 3], [4, 5, 6, 7]]; const secondArr = [[545, 65, 5, 7], [0, 0, 0, 0], []]; const getAverage = (arr) => { ...
Read MoreJavaScript symbol.description property
The symbol.description property in JavaScript returns the description string of a Symbol. Unlike Symbol.toPrimitive, the description property provides access to the optional description passed when creating a Symbol. Syntax symbol.description Return Value Returns the description string of the Symbol, or undefined if no description was provided during Symbol creation. Example: Symbol with Description Symbol Description Example Click to display symbol description... Show Description function display() { const sym1 = Symbol('user-id'); const ...
Read MoreHow can I remove a child node in HTML using JavaScript?
In JavaScript, you can remove child nodes from HTML elements using several methods. The most common approaches are removeChild() and the modern remove() method. Using removeChild() Method The removeChild() method removes a specified child node from its parent element. You need to call it on the parent element and pass the child node as a parameter. Remove Child Node body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result ...
Read MoreIf string includes words in array, remove them JavaScript
In JavaScript, you may need to remove specific words from a string based on an array of target words. This is useful for content filtering, text cleaning, or removing unwanted terms from user input. Problem Overview We need to create a function that removes all occurrences of words present in an array from a given string, while handling whitespace properly to avoid multiple consecutive spaces. Method 1: Using reduce() with Regular Expressions const string = "The weather in Delhi today is very similar to the weather in Mumbai"; const words = [ ...
Read MoreCall a JavaScript class object with variable?
In JavaScript, you can call a class constructor with variables by passing the variable as an argument. There are several ways to achieve this, including direct instantiation and using eval(). Basic Class Instantiation with Variables The most straightforward approach is to pass variables directly to the class constructor: class FirstClass { constructor(message) { this.message = message; } } var message = 'This is the Class Demo'; var object = new FirstClass(message); console.log(object.message); This is the Class ...
Read MoreFinding least number of notes to sum an amount - JavaScript
Suppose, we have a currency system where we have denominations of 1000 units, 500 units, 100 units, 50 units, 20 units, 10 units, 5 units, 2 units and 1 unit. Given a specific amount, we are required to write a function that calculates the least number of total denominations that sum up to the amount. For example, if the amount is 512, The least number of notes that will add up to it will be: 1 unit of 500, 1 unit of 10 and 1 unit of 2. So, for 512, our function should ...
Read MoreHow can we validate decimal numbers in JavaScript?
Validating decimal numbers in JavaScript is essential for form validation and data processing. There are several reliable methods to check if a value is a valid decimal number. Method 1: Using Regular Expression Regular expressions provide precise control over decimal number validation patterns: Decimal Validation Decimal Number Validation Validate ...
Read More