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
Front End Technology Articles
Page 301 of 652
Destructively 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 MoreDynamic Programming - Part sum of elements JavaScript
In this problem, we calculate the sum of all elements except one element at each step, creating an array of partial sums. We'll implement this using JavaScript array methods and dynamic programming concepts. Problem Statement Given an array of n elements, generate an array containing: The sum of all elements The sum of all elements except the first element The sum of all elements except the second element And so on for each element Algorithm Step 1: Calculate the total sum of all elements in the array. Step 2: Create a result array starting ...
Read MoreFetch Numbers with Even Number of Digits JavaScript
In JavaScript, finding numbers with an even count of digits means identifying numbers where the total digit count is divisible by 2. This is different from finding even numbers themselves. Understanding the Problem We need to create a function that takes an array of numbers and returns only those numbers that have an even number of digits. For example, in the array [12, 345, 67, 8910, 11, 9], the numbers 12 (2 digits), 67 (2 digits), and 8910 (4 digits) have even digit counts. Method 1: Using String Length Convert each number to a string and ...
Read MoreFinding longest substring between two same characters JavaScript
In this problem, we need to find the longest substring between two same characters in a string using JavaScript. We'll use a Map to track character positions and calculate distances between matching characters. Problem Explanation Given a string like 'abcdefa', we have two 'a' characters with 'bcdef' between them (5 characters). This becomes our longest substring between same characters. Algorithm Steps Step 1 − Define function longestSubstring with parameter 'str'. Step 2 − Initialize maxLength = 0 and begin = -1 to track the longest substring. Step 3 − Create a Map called charMap to store ...
Read MoreFunction to find out palindrome strings JavaScript
In this problem statement, our aim is to create a function to find out that the given string is palindrome with the help of Javascript functionalities. So for solving this problem first we need to understand the problem in simple terms. Understanding the Problem Statement We have given a string as an input string and our main aim is to check if the string is a palindrome string or not. If it is palindrome then return true otherwise return false. What is a Palindrome? A palindrome is a string that reads the same forwards and backwards. ...
Read MoreFunction to reverse a string JavaScript
In JavaScript, reversing a string is a common programming task that can be accomplished using various approaches. This tutorial demonstrates how to create a function that reverses any given string by iterating through its characters in reverse order. Understanding the Problem String reversal means rearranging the characters of a string so that the last character becomes first, second-last becomes second, and so on. For example, if we have the string "Hello", the reverse will be "olleH". Algorithm Step 1 − Declare a variable to store the reversed string and initialize it as empty. Step 2 − ...
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 MoreGet key from value in JavaScript
In JavaScript, finding a key from its corresponding value in an object requires iterating through the object's key-value pairs. Unlike accessing values by keys (which is direct), reverse lookup needs a search operation. Understanding the Problem Given a JavaScript object and a value, we need to find the key that maps to that value. For example: { key1: 'value1', key2: 'value2', key3: 'value3' } If we search for 'value2', we should get 'key2' as the result. Using Object.entries() and find() The most efficient ...
Read MoreGiven an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
Finding the pair of adjacent elements with the largest product is a common array problem in JavaScript. This involves iterating through the array and comparing products of consecutive elements. Problem Understanding Given an array of integers, we need to find two adjacent elements whose product is maximum among all possible adjacent pairs. For example, in array [1, 2, 3, 4], the adjacent pairs are (1, 2), (2, 3), and (3, 4) with products 2, 6, and 12 respectively. Method 1: Using -Infinity for Initialization This approach initializes the maximum product with -Infinity to handle arrays with ...
Read MoreHow to write the factorial function with reduce and range in JavaScript?
In this tutorial, we'll learn how to calculate factorials using JavaScript's reduce() method combined with array generation techniques. This approach demonstrates functional programming concepts while solving a common mathematical problem. Understanding reduce() and range Functions The reduce() function processes an array and reduces it to a single value by applying a function to each element. It takes an accumulator (which stores the result) and the current array value as parameters. const nums = [1, 2, 3, 4, 5]; const sum = nums.reduce((acc, val) => acc + val, 0); console.log(sum); 15 For ...
Read More