Articles on Trending Technologies

Technical articles with clear explanations and examples

Clear element.classList in JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 1K+ Views

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 More

Add time to string data/time - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

Calculate the hypotenuse of a right triangle in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 795 Views

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 More

Reversing strings with a twist in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

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 More

Longest string consisting of n consecutive strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 455 Views

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 More

Generating desired pairs within a range using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

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 More

How to shift the baseline of individual characters in Text using FabricJS?

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 428 Views

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 More

How to set the duration of animation on a Line using FabricJS?

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 393 Views

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 More

How to view array of a structure in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 459 Views

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 More

JavaScript Algorithm - Removing Negatives from the Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

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
Showing 16221–16230 of 61,297 articles
Advertisements