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
Find the greatest product of three numbers in JavaScript
In JavaScript, finding the greatest product of three numbers from an array requires considering both positive and negative numbers. This problem has multiple approaches, from brute force to optimized sorting methods. Understanding the Problem Given an array of integers, we need to find three numbers whose product is the largest among all possible combinations. For example, with array [1, 5, 3, 2, 4], the three largest numbers (3, 4, 5) give us the product 3 × 4 × 5 = 60. ...
Read MoreMapping values to keys JavaScript
In JavaScript, mapping values to keys means creating associations between identifiers (keys) and their corresponding data (values). This is commonly achieved using objects, which store data in key-value pairs for efficient retrieval. Understanding the Problem Objects in JavaScript are data structures that store information as key-value pairs. Keys serve as identifiers to access their associated values. There are several ways to access these values, with dot notation and bracket notation being the most common approaches. For example, with an object const obj = {name: 'John', age: 30}, you can access the name using obj.name or obj['name']. ...
Read MoreLargest 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 More