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 AmitDiwan
Page 358 of 840
Calculate Subtraction of diagonals-summations in a two-dimensional matrix using JavaScript
Suppose we have a square matrix represented by a 2-D array in JavaScript like this: const arr = [ [1, 3, 5], [3, 5, 7], [2, 4, 2] ]; console.log(arr); [ [ 1, 3, 5 ], [ 3, 5, 7 ], [ 2, 4, 2 ] ] We need to write a JavaScript function that calculates the absolute difference between the sum of elements on the two diagonals of the matrix. Understanding the Diagonals In a square matrix, there are two diagonals: ...
Read MoreRepeat String in JavaScript?
JavaScript provides multiple ways to repeat a string. The most modern approach is using the built-in repeat() method, but you can also use older techniques like Array.join(). Using String.repeat() (Recommended) The String.repeat() method is the standard way to repeat strings in modern JavaScript: String Repeat Example let str = "Hello "; let repeated = str.repeat(3); console.log(repeated); ...
Read MoreHide a video tag on a web page - JavaScript
In JavaScript, you can hide a video element on a web page by setting its display CSS property to "none" using the style.display property. Let's say we have the following sample video tag on a web page: You cannot play video here...... Syntax To hide a video element, use the following syntax: element.style.display = "none"; Method 1: Hide by Class Name This method selects the video element by its class name and hides it: ...
Read MoreJavaScript - Merge two arrays according to id property
When working with JavaScript arrays of objects, you often need to merge two arrays based on a common property like an ID. This is useful for combining related data from different sources, such as user profiles and addresses. Suppose we have two arrays of objects. The first contains user IDs and names, while the second contains user IDs and addresses: const arr1 = [ {"id":"123", "name":"name 1"}, {"id":"456", "name":"name 2"} ]; const arr2 = [ {"id":"123", "address":"address 1"}, {"id":"456", "address":"address 2"} ...
Read MoreMasking email to hide it in JavaScript
It is a common practice that when websites display anyone's private email address they often mask it in order to maintain privacy. For example, if someone's email address is: const email = 'ramkumar@example.com'; It is displayed like this: r...r@example.com We need to write a JavaScript function that takes an email string and returns the masked email for that string. Basic Email Masking Function Here's a simple approach that masks the username part of the email: const email = 'ramkumar@example.com'; const maskEmail = (email = '') => ...
Read MoreFinding the missing number between two arrays of literals in JavaScript
Finding the missing number between two arrays is a common programming problem where one array is a shuffled version of another with exactly one element removed. Problem Statement We need to write a JavaScript function that takes two arrays: arr1 (original) and arr2 (shuffled duplicate with one missing element). The function should identify and return the missing element. Example const arr1 = [6, 1, 3, 6, 8, 2]; const arr2 = [3, 6, 6, 1, 2]; const findMissing = (arr1 = [], arr2 = []) => { const obj = ...
Read MoreSorting 2-D array of strings and finding the diagonal element using JavaScript
Problem We are required to write a JavaScript function that takes in an array of n strings. Each string in the array consists of exactly n characters. Our function should first sort the array in alphabetical order, then return the string formed by the characters present at the principal diagonal starting from the top left corner. Understanding the Process The solution involves two main steps: Sort the array of strings alphabetically Extract diagonal characters where row index equals column index (i === j) Example ...
Read MoreHow to validate if an element in an array is repeated? - JavaScript
We are required to write a JavaScript function that takes in two arguments: An Array, say arr, of literals that may contain some repeating elements. A number, say limit. The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise. Using Object to Count Occurrences The most efficient approach is to use an object to count each element's occurrences, then check if any count exceeds ...
Read MoreExcluding extreme elements from average calculation in JavaScript
We need to write a JavaScript function that calculates the average of array elements while excluding the smallest and largest values. This is useful when you want to remove outliers from statistical calculations. Problem Statement Given an array of numbers, calculate the average excluding the minimum and maximum values. This helps eliminate extreme values that might skew the results. Example Let's implement a function that finds the excluded average: const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const findExcludedAverage = arr => { const creds ...
Read MoreRemoving duplicates and keep one instance in JavaScript
In JavaScript, you often need to remove duplicate values from an array while keeping only one instance of each value. There are several effective methods to accomplish this task. Method 1: Using Set (Recommended) The most modern and efficient approach uses the Set object, which automatically removes duplicates: const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; // Using Set to remove duplicates const uniqueArr = [...new Set(arr)]; console.log(uniqueArr); [ 1, 5, 7, 4, 6, 8 ] Method 2: Using filter with indexOf ...
Read More