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
Front End Technology Articles
Page 281 of 652
How to check two numbers are approximately equal in JavaScript?
In JavaScript, determining if two floating-point numbers are "approximately equal" is essential because of precision issues with decimal calculations. We compare the absolute difference between two numbers against a tolerance value called epsilon. Why Use Approximate Equality? Floating-point arithmetic can produce unexpected results: console.log(0.1 + 0.2); // 0.30000000000000004 console.log(0.1 + 0.2 === 0.3); // false 0.30000000000000004 false The Algorithm We calculate the absolute difference between two numbers and compare it with epsilon (tolerance). If the difference is less ...
Read MoreHow to create HTML list from JavaScript array?
In this tutorial, we will explore multiple ways to create an HTML list from a JavaScript array. While you can manually create HTML lists using ul (unordered list) and li (list item) tags, this becomes impractical when dealing with dynamic data or large arrays. JavaScript provides efficient methods to dynamically generate HTML lists from array data. Let's examine three different approaches to accomplish this task. Method 1: Using the for Loop The simplest approach uses a traditional for loop to iterate through the array and create list items dynamically using createElement() and appendChild(). ...
Read MoreHow to swap two array elements in JavaScript?
In this tutorial, we explore different approaches to swap two array elements in JavaScript. For example, we can swap the first and second elements of an array: Input: ["first", "second", "third", "fourth", "fifth"] Output: ["second", "first", "third", "fourth", "fifth"] Here we swapped "first" and "second" values of the array. We will explore three different methods to swap array elements: Using a temporary variable Using Array Destructuring (ES6) Using Array splice() Method Using a Temporary Variable ...
Read MoreHow to Create an Analog Clock using HTML, CSS, and JavaScript?
In this tutorial, we will create a functional analog clock using HTML, CSS, and JavaScript. The clock will display the current time with three hands representing hours, minutes, and seconds that move in real-time. Approach Create the clock structure using HTML with three div elements for the hands Style the clock face and hands using CSS positioning and transforms Use JavaScript's Date object to get current time and calculate hand rotations Update the clock every second using setInterval() Understanding the Time Calculations ...
Read MoreHow to convert 3-digit color code to 6-digit color code using JavaScript?
In this tutorial, we will see how to convert 3-digit color code to 6-digit color code in JavaScript. A 3-digit hex color code represents colors in #RGB format, where each digit controls red, green, and blue values. To convert to 6-digit format, we duplicate each character: #RGB becomes #RRGGBB. For example: 3-digit: #08c 6-digit: #0088cc 3-digit: #f4a 6-digit: #ff44aa Algorithm Step 1: Check if the input length is exactly 3 characters Step 2: Split the string into individual characters using split("") Step 3: Use map() to duplicate each character ...
Read MoreHow to change string to be displayed as a superscript using JavaScript?
In this tutorial, we will learn how to display strings as superscript using JavaScript and HTML. Superscript text appears raised above the normal text baseline, making it smaller and positioned at half-height. This is commonly used in mathematical formulas like A2, B3, or scientific notations like 105. Superscript is essential for writing mathematical expressions, chemical formulas (like H2O+), and footnote references in web content. Using HTML Tag The simplest way to create superscript text is using the HTML tag. Any text inside this tag will automatically display as superscript. Syntax 102 = ...
Read MoreHow to create a chessboard pattern with JavaScript and DOM?
Creating visual patterns with JavaScript and DOM manipulation is a great way to understand programming logic. In this tutorial, we'll create a classic chessboard pattern using JavaScript to dynamically generate alternating colored squares. Algorithm Overview The chessboard pattern relies on a simple mathematical principle: if the sum of row and column indices is even, the square is one color (black), otherwise it's another color (white). This creates the alternating pattern characteristic of a chessboard. Basic Implementation Here's how to create a chessboard pattern step by step: Create a container div for the chessboard Use ...
Read MoreHow to Create Fullscreen API using JavaScript?
The Fullscreen API is a browser API that allows developers to request fullscreen display from the user, and to exit fullscreen when desired. Using the Fullscreen API is relatively simple. First, you must check if the browser you are using supports the Fullscreen API. You can do this by checking for the presence of the requestFullscreen method on any element. If the browser does not support the Fullscreen API, you can still provide a fullscreen experience to your users by using another method, such as opening a new browser window. The function that requests full screen display ...
Read MoreHow to replace characters except last with a mask character in JavaScript?
JavaScript provides the replace() method to modify strings by replacing specific characters or patterns. A common requirement is to mask all characters in a string except the last one, which is useful for hiding sensitive data like credit card numbers or passwords. Syntax To replace all characters except the last one with a mask character, use this regex pattern: str.replace(/.(?=.)/g, maskCharacter); The regex /.(?=.)/g matches any character (.) that has at least one character after it ((?=.)), effectively selecting all characters except the last one. How It Works ...
Read MoreHow to negate a predicate function in JavaScript?
In JavaScript, a predicate function is a function that returns a Boolean value. In other words, it is a function that tests whether a certain condition is true or false. There are times when we need to negate a predicate function. That is, we need to return the opposite Boolean value. There are several ways to negate a predicate function in JavaScript. Using the ! operator (Logical NOT) The most common way to negate a predicate function is to use the ! operator. For example, consider the following predicate function − function isEven(num) ...
Read More