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 by Saurabh Anand
17 articles
JavaScript Program for Equilibrium Index of an Array
To write a JavaScript program for equilibrium index of an array, we are going to discuss three different approaches with detailed explanation and example codes. The equilibrium index of an array is the position where the sum of the elements to its left and right equals one another. In this article we are having an array of integers, our task is to write a JavaScript program for equilibrium index of an array. Example Input: arr = [-7, 1, 5, 2, -4, 3, 0] Output: Equilibrium index: 3 At index 3, left sum = -7 ...
Read MoreJavaScript Program to Find k Pairs with Smallest Sums in Two Arrays
To find k pairs with smallest sums in two arrays in JavaScript, we will be using sorted arrays in ascending order. We are going to discuss two different approaches with stepwise explanation and examples. In this article we are having two arrays and a value of k, our task is to find k pairs with smallest sums in two arrays. Users must be familiar with JavaScript arrays, loops and conditional statement. Example Input: arr1 = [1, 7, 11], arr2 = [2, 4, 6] k = 3 Output: [[1, 2], [1, 4], [1, 6]] Input: ...
Read MoreJavaScript Program for Find the smallest missing number
We are given a sorted array of distinct non-negative integers, here we have to find the smallest missing number. Hence in this tutorial, we will explore different methods to solve this problem and discuss their time complexities with various examples. Understanding the Problem The problem statement is quite simple. Given a sorted array of distinct non-negative integers, we need to find the smallest missing number in it. Let's take an example to understand the problem. Example Let's say that we have an array [1, 2, 4, 5, 6]. Here, We can see that there is a ...
Read MoreError.prototype.toString() Method in JavaScript
JavaScript's Error.prototype.toString() method is a built-in method that converts error objects to readable string representations. This method is essential for error handling, logging, and debugging in JavaScript applications. Error.prototype.toString() Method The Error.prototype.toString() method converts an error object to a string format. It returns a string containing the error name, followed by a colon and space, then the error message. For example, a standard Error object will produce a string like "Error: something went wrong". Syntax errorObject.toString() Parameters: None Return Value: A string representation of the error object Basic Usage Example Here's ...
Read MoreError: Permission denied to access property 'target' in JavaScript
"Error − Permission denied to access property 'target'" is a common error message that occurs when working with JavaScript, especially when attempting to access the target property of an event object. This error occurs when a script tries to access a property or method of an object that it does not have permission to access. In this tutorial, we will discuss the causes of this error and how to fix it. Understanding the Error The "Error − Permission denied to access property 'target'" is typically caused by the use of the target property of an event object in ...
Read MoreES2022 JavaScript at() Method
JavaScript ES2022 introduced the at() method for strings, providing a more robust way to retrieve characters at specific positions. Unlike the traditional charAt() method, at() supports negative indexing and handles edge cases more gracefully. Syntax string.at(index) Where string is the target string and index is the position (zero-based). Returns the character at the specified index, or undefined if the index is out of range. Basic Usage Here's how to retrieve the first character of a string: ...
Read MoreHow to convert special characters to HTML in Javascript?
Special characters like , and & have special meaning in HTML and can break your webpage if not properly escaped. JavaScript provides several methods to convert these characters into their HTML entity equivalents. Why Convert Special Characters to HTML? When displaying user input or dynamic content on a webpage, special characters can cause problems. For example, if you want to display "" as text, the browser might interpret it as an actual HTML tag. Converting these characters to HTML entities like ensures they display as intended text. Common characters that need escaping: ...
Read MoreHow to convert UTC date time into local date time using JavaScript?
Timezone handling is an essential component of every web application. The time that is recorded in the backend is usually in UTC format. When it is displayed to the user, however, it must be converted to the user's local time. This is possible with JavaScript. We'll look at how to use JavaScript to convert UTC date time to local date time in this blog. JavaScript Date Object JavaScript includes a "Date" object that allows us to work with dates and times. The Date object includes various methods for working with dates and times, including: ...
Read MoreHow to count text lines inside of a DOM element?
Introduction The DOM (Document Object Model) is an API for HTML and XML documents that provides programmers control over a webpage's structure, appearance, and content. Counting text lines within DOM elements is useful for layout calculations, pagination, and content management. This article explores three methods to count text lines in DOM elements using JavaScript. Method 1: Using the scrollHeight Property The scrollHeight property returns the total height of an element, including content hidden by overflow. We can calculate the number of lines by dividing scrollHeight by the height of a single line of text. ...
Read MoreHow to count the number of times a button is clicked using JavaScript?
Tracking button clicks is a common task in JavaScript and can be achieved by using the addEventListener() method. By adding an event handler to the button element and incrementing a variable each time the button is pressed, we can keep track of button clicks. We can display the count to users and even persist clicks using localStorage so they remain after browser closure. This technique is essential for interactive web applications like calculators, games, or analytics tracking. Let's explore different methods to count button clicks effectively. Creating a Basic Button First, we need a button element on ...
Read More