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 Nikitasha Shrivastava
Page 11 of 17
Largest difference between element with a twist in JavaScript
In this problem, we need to find the largest difference between elements with a twist using JavaScript. The twist is that we can only calculate the difference between an element and any smaller element that appeared before it in the array. Understanding the Problem Unlike finding the simple maximum difference between any two elements, this problem requires us to: Maintain the original array order (no sorting) Only consider differences where the larger element comes after the smaller one Find the maximum possible difference under these constraints ...
Read MoreDestructively Sum all the digits of a number in JavaScript
Our main task is to write a function that destructively sums all the digits of a number using JavaScript. This approach modifies the original number during the calculation process by extracting each digit until none remain. Understanding the Problem The goal is to create a function that calculates the sum of all digits in a given number. The term "destructive" means the original number is modified during the process as we extract each digit. For example, given the number 123, we need to calculate: 1 + 2 + 3 = 6. Algorithm Step 1 − ...
Read MorePartial sum in array of arrays JavaScript
In the given problem statement our task is to get the partial sum in an array of arrays with the help of JavaScript functionalities. The partial sum at position (i, j) represents the sum of all elements from the top-left corner (0, 0) to the current position (i, j). Understanding the Problem An array of arrays (2D array) is a data structure where each element is itself an array. For example: [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] The partial ...
Read MoreRetrieve key and values from object in an array JavaScript
In JavaScript, extracting keys and values from objects within an array is a common task when processing data structures. This can be accomplished using built-in Object methods like Object.keys() and Object.values(). Understanding the Problem When working with arrays of objects, you often need to access both the property names (keys) and their corresponding values. JavaScript objects are collections of key-value pairs, and the Object class provides methods to extract this information efficiently. Using Object.keys() and Object.values() The Object.keys() method returns an array of property names, while Object.values() returns an array of property values from an object. ...
Read MoreMasking a string JavaScript
Masking a string means replacing certain characters with asterisks (*) or other symbols to hide sensitive information like passwords, credit card numbers, or personal data. In JavaScript, we can create a function to mask specific portions of a string while keeping the rest visible. Understanding String Masking String masking is commonly used in applications to protect sensitive data while still showing partial information for verification purposes. For example, displaying a credit card number as "1234-****-****-5678" or an email as "j***@example.com". Basic Masking Function Here's a function that masks characters between specified start and end positions: ...
Read MoreGet all substrings of a string in JavaScript recursively
In JavaScript, generating all substrings of a string can be elegantly solved using recursion. A substring is any continuous sequence of characters within a string. For example, the string "abc" has substrings: "a", "b", "c", "ab", "bc", and "abc". Understanding the Problem We need to create a recursive function that generates all possible substrings of a given input string. For the string "xy", the possible substrings are "x", "y", and "xy". The recursive approach systematically explores all starting and ending positions to build each substring. Algorithm Steps Step 1 − Define the main function that accepts ...
Read MoreRound number down to nearest power of 10 JavaScript
In JavaScript, rounding down a number to the nearest power of 10 involves finding the highest power of 10 that is less than or equal to the given number. For example, 1365 rounds down to 1000, and 987 rounds down to 100. Understanding the Problem The task is to create a function that rounds down any given number to the nearest power of 10. Powers of 10 are numbers like 1, 10, 100, 1000, etc. For a number like 1365, the nearest lower power of 10 is 1000, while for 87, it would be 10. Algorithm ...
Read MoreConvert string with separator to array of objects in JavaScript
In this article, we'll learn how to convert a string with separators into an array of objects using JavaScript's built-in methods. This is a common data transformation task when working with CSV-like data or user input. Understanding the Problem We need to transform a string like "name1, name2, name3" with separator ", " into an array of objects: [{value: "name1"}, {value: "name2"}, {value: "name3"}] Each substring becomes an object with a value property containing the original string segment. Algorithm Step 1: Split the input string using the separator to create an array ...
Read MoreRemove duplicate items from an array with a custom function in JavaScript
In JavaScript, arrays often contain duplicate values that need to be removed. This article demonstrates how to create a custom function to eliminate duplicates and return an array with unique elements only. Understanding the Problem We need to create a function that accepts an array as input and returns a new array containing only unique items, excluding any duplicates. For example, given the array [0, 0, 2, 2, 3, 3], the function should return [0, 2, 3]. Algorithm Overview The algorithm iterates through the input array and adds items to a result array only if they ...
Read MoreJavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
For a given list of elements, write a JavaScript program to find the sum of its odd and even indexed elements and then, calculate difference between them. To solve this problem, we will first separate odd indexed and even indexed elements. After separating them find their sum separately and store it in different variables. Now, we will calculate the difference between these sums to get desired result. Example Scenario: Input: list = [11, 21, 31, 41, 51, 61]; Output: difference = 30 Here, odd-indexed items are [21, 41, 61] and their sum is ...
Read More