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
The Fibonacci sequence in Javascript
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The sequence starts with 1, 1 (or sometimes 0, 1). 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Naive Recursive Approach The most straightforward way to generate the nth Fibonacci number uses recursion: function fibNaive(n) { if (n
Read MoreHow do I subtract one week from this date in JavaScript?
To subtract one week (7 days) from a date in JavaScript, use the setDate() method combined with getDate() to modify the date object directly. Syntax var newDate = new Date(currentDate.setDate(currentDate.getDate() - 7)); Method 1: Modifying Current Date First, get the current date, then subtract 7 days using setDate(): var currentDate = new Date(); console.log("The current Date = " + currentDate); var before7Daysdate = new Date(currentDate.setDate(currentDate.getDate() - 7)); console.log("The One week ago date = " + before7Daysdate); The current Date = Tue Jul 14 2020 19:12:43 GMT+0530 (India Standard ...
Read MoreAdding a function for swapping cases to the prototype object of strings - JavaScript
In JavaScript, we can extend built-in data types by adding custom methods to their prototype objects. This allows us to create reusable functions that work with existing data types like strings, arrays, and objects. In this tutorial, we'll create a custom swapCase() method for strings that swaps the case of each character - converting uppercase letters to lowercase and vice versa, while keeping non-alphabetic characters unchanged. Understanding String Prototype Extension When we add a method to String.prototype, it becomes available to all string instances. The this keyword inside the method refers to the string that called the ...
Read MoreHow to replace leading zero with spaces - JavaScript
We are required to write a JavaScript function that takes in a string that represents a number. Replace the leading zero with spaces in the number. We need to make sure the prior spaces in number are retained. For example, If the string value is defined as − " 004590808" Then the output should come as − " 4590808" Example Following is the code − const str = ' 004590808'; const replaceWithSpace = str => { let replaced = ''; const regex = new RegExp(/^\s*0+/); ...
Read MoreHow to reset or clear a form using JavaScript?
This tutorial teaches us how to reset or clear a form using JavaScript. This option is very helpful for users when they have filled the wrong data and want to clear everything at once. The reset() method provides an efficient way to restore all form fields to their initial values. The reset() method is a built-in JavaScript function that clears all input fields in a form and restores them to their default values. Syntax document.getElementById("formId").reset(); // or document.forms["formName"].reset(); The getElementById() method retrieves the form element by its ID, and then reset() clears all form ...
Read MoreHow to get current date/time in seconds in JavaScript?
To get the current time in seconds in JavaScript, you can extract hours, minutes, and seconds from a Date object and convert them to total seconds using the formula: (hours * 3600) + (minutes * 60) + seconds Method 1: Converting Current Time to Seconds This approach gets individual time components and calculates total seconds since midnight: JavaScript Get Seconds ...
Read MoreHow to convert a negative number to a positive one in JavaScript?
This tutorial will teach us to convert negative numbers to positive ones. Sometimes, programmers need to perform operations with the unsigned value of a number, and in such cases, they need to convert negative numbers to positive numbers. We will explore three different methods to convert negative numbers to positive numbers in JavaScript: Using the Math.abs() Method Multiplying the negative number with -1 Using the Bitwise Not Operator Using the Math.abs() Method The Math.abs() method is the most commonly used approach. It ...
Read MoreHow -Infinity is converted to Boolean in JavaScript?
In JavaScript, -Infinity is considered a truthy value and converts to true when converted to Boolean. This might be surprising since negative infinity seems conceptually "negative, " but JavaScript treats all non-zero numbers (including infinities) as truthy. Using Boolean() Constructor The Boolean() constructor explicitly converts values to their boolean equivalent: Convert -Infinity to Boolean var myVal = -Infinity; document.write("Boolean: ...
Read MoreHow to apply antialiasing in HTML5 canvas drawImage()?
HTML5 Canvas provides built-in antialiasing through image smoothing properties to improve the quality of scaled images rendered with drawImage(). Setting Image Smoothing Quality The primary way to control antialiasing is through the imageSmoothingQuality property: const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // Set antialiasing quality ctx.imageSmoothingQuality = "high"; // "low", "medium", or "high" // Create a small test image const img = new Image(); img.onload = function() { // Draw scaled image with antialiasing ctx.drawImage(img, 0, 0, 200, 150); }; img.src = ...
Read MoreDepth-first search traversal in Javascript
DFS visits the child vertices before visiting the sibling vertices; that is, it traverses the depth of any particular path before exploring its breadth. A stack (often the program's call stack via recursion) is generally used when implementing the algorithm. Following is how a DFS works − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack. If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices from the stack, which do not ...
Read More