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
How to adjust the Width of Input Field Automatically using JavaScript?
We can use JavaScript to adjust the width of an input field automatically. This can be done by using the element's style property to set the width based on the input's value. By constantly updating the width as the user types, we can ensure that the input field is always the appropriate size for the input. Basic Approach Here is one approach to adjust the width of an input field automatically using JavaScript: Select the input field using JavaScript, for example, by using the document.querySelector() method: var inputField = document.querySelector("#myInput"); ...
Read MoreHow to remove some items from array when there is repetition in JavaScript
In JavaScript, you may need to remove items from an array that appear in multiples of three (triplets). This is useful when you want to keep only the remaining elements after removing complete sets of triplets. Understanding the Problem The goal is to remove triplets (groups of 3 identical elements) from an array and keep only the remaining elements. For example, if an array has 5 occurrences of the number 1, we remove 3 of them (one triplet) and keep 2. Solution Implementation const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = ...
Read MoreCheck for perfect square in JavaScript
We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. For example, 16 is a perfect square because 4 × 4 = 16. Examples of Perfect Square Numbers 144 (12²), 196 (14²), 121 (11²), 81 (9²), 484 (22²) Method 1: Using Math.sqrt() The most straightforward approach is to find the square root and check if ...
Read MoreFinding perfect numbers in JavaScript
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. For example: 28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14 We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number. How Perfect Numbers Work To check if a number is perfect, we need to: ...
Read MoreImplementing Heap Sort using vanilla JavaScript
Heap Sort is a comparison-based sorting algorithm that can be thought of as an improved selection sort. Like selection sort, it divides input into sorted and unsorted regions, but uses a binary heap data structure to efficiently find the maximum (or minimum) element. How Heap Sort Works Heap Sort works in two main phases: Build Max Heap: Convert the array into a max heap where parent nodes are larger than their children Extract Elements: Repeatedly remove the maximum element (root) and restore the heap property Implementation Here's a complete implementation of Heap Sort ...
Read MoreArranging words by their length in a sentence in JavaScript
In JavaScript, you can arrange words in a sentence by their length using string manipulation and array sorting methods. This technique is useful for text processing and formatting applications. A sentence is a string of characters joined by whitespaces. The goal is to rearrange words so the shortest word appears first, followed by progressively longer ones. Problem Example If the input string is: const str = 'this is a string'; Then the output should be: const output = 'a is this string'; Solution Using Array Methods Here's a ...
Read MoreRemoving adjacent duplicates from a string in JavaScript
In JavaScript, removing adjacent duplicate characters from a string involves iterating through the string and eliminating consecutive identical characters until no more duplicates remain. This problem is commonly solved using a stack-based approach. Problem Statement Given a string, we need to repeatedly remove adjacent duplicate characters until no more adjacent duplicates exist. For example, with the string 'kllkmk', we first remove 'll' to get 'kkmk', then remove 'kk' to get the final result 'mk'. Algorithm Approach We use a stack (array) to track characters. When we encounter a character that matches the top of the stack, ...
Read MoreFinding path to the end of maze using JavaScript
Problem We are required to write a JavaScript function that takes in a matrix of N * N order. The walls in the matrix are marked by 'W' and empty positions are marked by '_'. We can move in any of the four directions at any point. Our function should return true if we can reach the end position [N - 1, N - 1] from the starting position [0, 0], false otherwise. Algorithm Approach We'll use a Breadth-First Search (BFS) algorithm to find if a path exists from the starting position to the end position. ...
Read MoreHow to convert a value to a safe integer using JavaScript?
An integer is known to be a safe integer if it can be exactly represented as an IEEE-754 double precision number. A safe integer is an integer in the range -(2^53 - 1) to (2^53 - 1). All these numbers are considered safe integers because they allow one-to-one mapping between mathematical integers and their representation in JavaScript. What is Number.isSafeInteger()? The Number.isSafeInteger() method determines whether a value is a safe integer. It returns true if the value is a safe integer, otherwise false. Syntax Number.isSafeInteger(testValue) Here testValue is the number to check if ...
Read MoreFabricJS – How to identify if the given object is of a Polygon instance?
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to identify if the given object is of a Polygon instance, we use the isType method. This method checks if the object is of the specified type and returns a true or false value depending on that. Syntax isType(type: String): ...
Read More