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
Clear element.classList in JavaScript?
The task we are going to perform in this article is clear element classList in JavaScript. The classList property provides methods to add, remove, toggle, and check CSS classes on HTML elements. All element objects (i.e., objects that represent elements) in a Document derive from the most general base class called Element. It contains functions and characteristics that apply to all types of elements. What is classList in JavaScript JavaScript classList is a DOM property that enables manipulation of an element's CSS classes. The read-only classList property returns a DOMTokenList containing the CSS class names of an ...
Read MoreAdd time to string data/time - JavaScript?
In JavaScript, you can add time to a date/time string using the Date object's built-in methods. This is useful for calculating future times or adjusting existing timestamps. Basic Approach First, create a Date object from your string, then use methods like setHours(), setMinutes(), or setSeconds() combined with their getter counterparts to add time. var dateValue = new Date("2021-01-12 10:10:20"); dateValue.setHours(dateValue.getHours() + 2); // Add 2 hours Example: Adding Hours to Date String var dateValue = new Date("2021-01-12 10:10:20"); console.log("Original date: " + dateValue.toString()); // Add 2 hours dateValue.setHours(dateValue.getHours() + 2); ...
Read MoreCalculate the hypotenuse of a right triangle in JavaScript
We are required to write a JavaScript function that takes in two numbers. The first number represents the length of the base of a right triangle and the second is perpendicular. The function should then compute the length of the hypotenuse based on these values. For example − If base = 8, perpendicular = 6 Then the output should be 10 The Pythagorean Theorem The hypotenuse is calculated using the Pythagorean theorem: c² = a² + b², where c is the hypotenuse and a, b are the other two sides. ...
Read MoreReversing strings with a twist in JavaScript
We are required to write a JavaScript function that takes in a string str as the first argument and an integer num as the second argument. Our function should reverse the first num characters for every 2 * num characters counting from the start of the string. If there are less than num characters left, we have to reverse all of them. If there are less than 2 * num but greater than or equal to num characters, then we have to reverse the first num characters and leave the others as original. Example Input and Output ...
Read MoreLongest string consisting of n consecutive strings in JavaScript
Problem We are required to write a JavaScript function that takes in an array of strings. Our function should create combinations by combining all possible n consecutive strings in the array and return the longest such string that comes first. Example Following is the code − const arr = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; const num = 2; function longestConsec(strarr, k) { if (strarr.length == 0 || k > strarr.length || k longStr.length) { ...
Read MoreGenerating desired pairs within a range using JavaScript
We are required to write a JavaScript function that takes in a number n. Our function should generate an array containing the pairs of integers [a, b] that satisfy the following conditions: 0
Read MoreHow to shift the baseline of individual characters in Text using FabricJS?
In this tutorial, we are going to learn how to shift the baseline of individual characters in Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. Similarly, we can also shift the baseline of individual characters by using the deltaY property. Syntax new fabric.Text(text: String , { styles: { deltaY: ...
Read MoreHow to set the duration of animation on a Line using FabricJS?
In this tutorial, we are going to learn how to set the duration of animation on a Line 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 change the duration of animated text we use the duration property. Syntax ...
Read MoreHow to view array of a structure in JavaScript?
The easiest way to debug JavaScript code is using console.log(), which many developers use. Sometimes, we need to know an array's structure and stored values for debugging purposes. In this tutorial, we will learn to view the structure of arrays containing different data types. Various JavaScript methods allow us to examine the structure of arrays. For example, we can see if an array contains objects, nested arrays, strings, numbers, or Boolean values. Using the JSON.stringify() Method The JSON.stringify() method converts JavaScript objects into JSON strings. Since arrays are objects in JavaScript, we can use this method to ...
Read MoreJavaScript Algorithm - Removing Negatives from the Array
Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), we need to write a function that removes any negative values from the array using only the pop() method without creating temporary arrays. The challenge is to modify the original array in-place, maintaining only positive numbers while using just the pop() method for removal. Algorithm Approach The algorithm works in two phases: Remove all negative values from the end of the array For remaining negatives, replace them with the last positive element and pop Complete Implementation function ...
Read More