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
Remove element by id in JavaScript?
Removing elements by ID is a fundamental DOM manipulation technique in JavaScript. You can remove elements using getElementById() to select the element and remove() to delete it from the page. JavaScript provides several ways to dynamically modify web page content. Removing elements by their ID is one of the most common operations when creating interactive web applications. Methods for Removing Elements by ID There are two main approaches: Using remove() method (Modern) - Direct element removal Using removeChild() method (Legacy) - Parent-based removal Method 1: Using getElementById() ...
Read MoreHow to count number of occurrences of repeated names in an array - JavaScript?
In JavaScript, counting occurrences of repeated names in an array is a common task when working with data analysis and statistics. This involves iterating through an array of objects and tracking how many times each name appears. Let's consider an array consisting of student details with repeated names: var details = [ { studentName: "John", studentAge: 23 }, { studentName: "David", studentAge: 24 ...
Read MoreRemoving duplicates and keep one instance in JavaScript
In JavaScript, you often need to remove duplicate values from an array while keeping only one instance of each value. There are several effective methods to accomplish this task. Method 1: Using Set (Recommended) The most modern and efficient approach uses the Set object, which automatically removes duplicates: const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; // Using Set to remove duplicates const uniqueArr = [...new Set(arr)]; console.log(uniqueArr); [ 1, 5, 7, 4, 6, 8 ] Method 2: Using filter with indexOf ...
Read MoreSwapcase function in JavaScript
In JavaScript, a swapcase function converts uppercase letters to lowercase and lowercase letters to uppercase while leaving non-alphabetic characters unchanged. Problem Statement We need to create a function that takes a string and returns a new string where: Uppercase letters become lowercase Lowercase letters become uppercase Non-alphabetic characters remain unchanged Method 1: Using Helper Function This approach uses a separate function to determine the case of each character: const str = 'ThIs Is A STriNG'; const findLetter = (char = '') => { if (char.toLowerCase() === ...
Read MoreDeleting occurrences of an element if it occurs more than n times using JavaScript
In JavaScript, managing array elements with excessive occurrences is a common data manipulation task. This article demonstrates how to delete elements that appear more than a specified number of times while preserving the original order. Problem Statement Given an array and a positive integer n, write a JavaScript function that removes elements if they occur more than n times. The first n occurrences should be kept, and any additional occurrences should be removed. Sample Input: const arr = [1, 2, 3, 1, 2, 1, 1, 3]; const n = 2; Sample Output: ...
Read MoreReading a text file into an Array in Node.js
Reading text files into arrays is a common task in Node.js applications. The built-in fs module provides both synchronous and asynchronous methods to read files and convert their content into arrays by splitting lines. Using fs.readFileSync() (Synchronous) The synchronous approach blocks code execution until the file is completely read. This method is suitable for smaller files or when you need the data immediately. // Importing the fs module const fs = require("fs"); // Function to read file and convert to array const readFileLines = filename => fs.readFileSync(filename) .toString('UTF8') .split(''); ...
Read MoreHow to return all matching strings against a RegEx in JavaScript?
In JavaScript, regular expressions (RegEx) are powerful tools for pattern matching in strings. While string.search() returns the position of the first match, there are multiple methods to return all matching strings or substrings against a RegEx pattern. Using string.search() Method The search() method returns the index of the first match, or -1 if no match is found. Syntax let index = string.search(expression) Parameters string − The string to be searched expression − The regular expression pattern to match Example ...
Read MoreHow to disable the selectability of a Rectangle using FabricJS?
In this article, we are going to learn how to disable selectability of a Rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect class and add it to the canvas. In order to modify an object, we have to select it in FabricJS. However, we can change this behaviour by using the selectable property. Syntax new fabric.Rect({ selectable: Boolean }: Object) Parameters Options (optional) ...
Read MoreHow to scale a Line object to a given height using FabricJS?
In this tutorial, we are going to learn how to scale a Line object to a given height using FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled. We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. In order to scale a Line object to a given height we use the scaleToHeight method. Syntax ...
Read MoreJavaScript Array Ranking
In this article, we will learn to rank elements in arrays using JavaScript. Ranking elements in an array is common in various applications, such as sorting scores, evaluating performance, or analyzing data. What is Array Ranking? Array ranking involves assigning a rank to each element of an array based on its value, where the largest value gets the highest rank and the smallest value gets the lowest rank. This ranking can be extended to multiple arrays to analyze and compare data across different datasets. Problem Statement We are given multiple arrays of numbers. The task is ...
Read More