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 Imran Alam
Page 2 of 6
How 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 MoreHow to implement enqueue and dequeue using only two stacks in JavaScript?
In this tutorial, we'll implement a queue using only two stacks. This is a common data structure interview question that demonstrates understanding of both queues (FIFO - First In, First Out) and stacks (LIFO - Last In, First Out). Algorithm STEP 1 − We have two stacks: one for enqueuing (input) and one for dequeuing (output). STEP 2 − We enqueue by pushing onto the enqueue stack. STEP 3 − We dequeue by popping from the dequeue stack. STEP 4 − ...
Read MoreHow to create infix to postfix converter in JavaScript?
An infix to Postfix converter is a tool that converts an infix expression to a postfix expression. In this tutorial, we will build the infix to postfix converter using JavaScript. What is an infix expression? Infix expression is an expression in which the operators are between the operands. For example, the expression "3 + 4" is an infix expression. What is a postfix expression? A postfix expression is an expression in which the operators are after the operands. For example, the expression "3 4 +" is a postfix expression. How does the Infix to Postfix converter work? ...
Read MoreWhat is Variable Shadowing in JavaScript?
In programming, shadowing occurs when a variable declared in a certain scope (e.g. a local variable) has the same name as a variable in an outer scope (e.g. a global variable). When this happens, the outer variable is said to be shadowed by the inner variable. In JavaScript, variables can be shadowed in both the global and function scope. Global variables can be shadowed by function-scoped variables, and function-scoped variables can be shadowed by block-scoped variables declared with the let or const keyword. Variable Shadowing in the Global Scope In the global scope, shadowing occurs when a ...
Read MoreHow to Generate Random Birthday Wishes using JavaScript?
In this tutorial, we will learn how to generate a random birthday wish using JavaScript. We will use the Math.random() method to generate a random number and select a birthday wish from an array of wishes. The Math.random() Method The Math.random() method is a built-in function in JavaScript that returns a random number between 0 (inclusive) and 1 (exclusive). console.log(Math.random()); // 0.6789234567 console.log(Math.random()); // 0.1234567890 0.6789234567 0.1234567890 Creating the Array of Birthday Wishes First, we'll create an array containing various birthday wishes: var wishes = [ ...
Read MoreHow to return an object by parsing http cookie header in JavaScript?
Cookies are small pieces of data that are sent from a website to a user's web browser. They are used to store information about the user, such as their preferences or login status. When a user visits a website, their web browser will send a request to the server. The server will then send a response, which includes a set of headers. One of these headers is the "Cookie" header, which contains a list of all the cookies that are associated with the website. Understanding Cookie Headers Cookie headers can contain multiple name-value pairs separated by semicolons. ...
Read MoreHow to make active tab in navigation menu using HTML CSS & JavaScript?
Creating an active tab navigation menu helps users understand which page or section they're currently viewing. This tutorial demonstrates how to build an interactive navigation menu using HTML, CSS, and JavaScript with proper active state management. HTML Structure The HTML structure uses a simple unordered list with navigation items. Each li element represents a tab, and anchor tags provide the clickable links. Home About Services Portfolio Contact CSS Styling The CSS creates the visual design and ...
Read MoreHow to filter values from an array using the comparator function in JavaScript?
In JavaScript, filtering an array can be done by using the filter() method. This method creates a new array with all elements that pass the test implemented by the provided callback function (comparator function). Syntax arr.filter(callback) Here callback is a comparator function used to test each element in the array arr. It should return true to keep the element, or false to filter it out. Using Array.filter() Method The most efficient way to filter values is using the built-in filter() method. Here's an example filtering odd numbers: ...
Read MoreHow to get index in a sorted array based on iterator function in JavaScript?
When working with sorted arrays in JavaScript, you often need to find the correct insertion position for a new element to maintain the sorted order. This article explores different methods to achieve this using iterator functions. Using Binary Search for Insertion Index The most efficient approach is to implement a binary search algorithm that finds the insertion point without modifying the original array. function findInsertionIndex(arr, value, compareFunction) { let left = 0; let right = arr.length; while (left < ...
Read More