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
Encrypting censored words using JavaScript
This tutorial demonstrates how to encrypt or mask censored words in JavaScript by applying specific transformation rules to convert regular text into a masked format. Problem We need to write a JavaScript function that takes in a string and converts it according to the following rules: All words should be in uppercase Every word should end with '!!!!' Any letter 'a' or 'A' should become '@' Any other vowel (E, I, O, U) should become '*' Solution Here's the implementation of the word masking function: const str = 'ban censored words'; ...
Read MoreImplement polyfill for String.prototype.trim() method in JavaScript
Some old versions of browsers don't support newly evolved JavaScript features. For example, very old browser versions don't support ES10 features like the Array.flat() method for flattening arrays. In such cases, we need to implement user-defined methods called polyfills to provide that functionality for older browsers. Here, we will implement polyfills for the String.prototype.trim() method. What is String.prototype.trim()? The trim() method removes whitespace characters from both ends of a string and returns a new string without modifying the original. Method 1: Using Regular Expression The most common approach uses a regular expression to match and ...
Read MoreSum of all the non-repeating elements of an array JavaScript
To find the sum of non-repeating elements in an array, we need to identify elements that appear exactly once and add them together. Problem Statement Given an array of numbers, we want to calculate the sum of all elements that appear only once in the array. const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]; console.log("Input array:", arr); Input array: [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14] In this array, the non-repeating elements are: 23, 24, 33, 44, 87. Their ...
Read MoreIf the element repeats, remove all its instances from array in JavaScript
When working with arrays in JavaScript, sometimes you need to remove all instances of elements that appear more than once, keeping only elements that appear exactly once. This is different from typical deduplication where you keep one instance of each duplicate. For example, if the input is: const arr = [763, 55, 43, 22, 32, 43, 763, 43]; console.log("Original array:", arr); Original array: [ 763, 55, 43, 22, 32, 43, 763, 43 ] The output should be: [ 55, 22, 32 ] Notice that 763 and 43 are ...
Read MoreJavaScript function that takes a multidimensional and a single array, and finds matches of the single array in the multi-d array
We need to write a JavaScript function that takes a multidimensional array (array of arrays) as the first argument and a single array as the second argument. The function should find common elements between each subarray and the single array, returning a new array containing only the matching elements from each subarray. Problem Understanding For each subarray in the multidimensional array, we want to find elements that also exist in the single array. The result preserves the structure but only includes common elements. For example, if we have: const arr1 = [ ...
Read MoreImplementing a Binary Search Tree in JavaScript
A tree is a collection of nodes connected by edges, where each node holds data and references to its children. Binary Search Trees (BST) are a special type of binary tree that maintains a sorted order. What is a Binary Search Tree? A Binary Search Tree is a binary tree where nodes with lesser values are stored on the left, and nodes with higher values are stored on the right. This property makes searching, insertion, and deletion operations efficient. 25 ...
Read MoreHow to replace line breaks with using JavaScript?
In this tutorial, we will learn how to replace line breaks in JavaScript strings with HTML tags. This is commonly needed when converting plain text with line breaks to HTML format for proper display in web pages. Using String replace() Method and RegEx The replace() method with regular expressions is the most comprehensive approach as it handles all types of line breaks including \r (Windows), (Unix/Linux), and \r (Mac). Syntax sentence.replace(/(?:\r|\r|)/g, ""); Here sentence is the string with line breaks, and the regular expression matches all three line break formats. ...
Read MoreSubarrays product sum in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each. The function should calculate the product of each subarray and then add both the results together. Example If the input array is: const arr = [1, 2, 3, 4, 5, 6] The calculation would be: (1*2*3) + (4*5*6) 6 + 120 126 Using Array.reduce() Method Here's ...
Read MoreArray flattening using loops and recursion in JavaScript
Array flattening converts nested arrays into a single-level array. JavaScript provides multiple approaches including loops, recursion, and built-in methods. Problem Overview Given a nested array with mixed data types including falsy values, we need to flatten it completely: const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; console.log("Input:", arr); Input: [ [ 1, 2, 3 ], [ 4, 5, [ 5, false, 6, [Array] ] ], [ 6 ] ] Expected output should be a flat array preserving all values including false and null: ...
Read MoreRemove the duplicate value from array with images data in JavaScript
When working with arrays of objects containing image data, you often need to remove duplicates based on a specific property. Here's how to remove duplicate objects based on the 'image' property value. Sample Data Consider an array of image objects where some images appear multiple times: const arr = [{ 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }, { ...
Read More