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 377 of 840
Arranging words by their length in a sentence in JavaScript
In JavaScript, you can arrange words in a sentence by their length using string manipulation and array sorting methods. This technique is useful for text processing and formatting applications. A sentence is a string of characters joined by whitespaces. The goal is to rearrange words so the shortest word appears first, followed by progressively longer ones. Problem Example If the input string is: const str = 'this is a string'; Then the output should be: const output = 'a is this string'; Solution Using Array Methods Here's a ...
Read MoreRemoving adjacent duplicates from a string in JavaScript
In JavaScript, removing adjacent duplicate characters from a string involves iterating through the string and eliminating consecutive identical characters until no more duplicates remain. This problem is commonly solved using a stack-based approach. Problem Statement Given a string, we need to repeatedly remove adjacent duplicate characters until no more adjacent duplicates exist. For example, with the string 'kllkmk', we first remove 'll' to get 'kkmk', then remove 'kk' to get the final result 'mk'. Algorithm Approach We use a stack (array) to track characters. When we encounter a character that matches the top of the stack, ...
Read MoreFinding path to the end of maze using JavaScript
Problem We are required to write a JavaScript function that takes in a matrix of N * N order. The walls in the matrix are marked by 'W' and empty positions are marked by '_'. We can move in any of the four directions at any point. Our function should return true if we can reach the end position [N - 1, N - 1] from the starting position [0, 0], false otherwise. Algorithm Approach We'll use a Breadth-First Search (BFS) algorithm to find if a path exists from the starting position to the end position. ...
Read MoreHow to align flexbox container into center using JavaScript?
To align a flexbox container to the center using JavaScript, you can manipulate the container's CSS properties through the DOM. This involves selecting the container element and applying flexbox properties to center its content horizontally and optionally vertically. Flexbox is a CSS3 layout mode that provides powerful alignment and distribution capabilities for elements on a page. It's particularly useful for creating responsive layouts, navigation bars, and galleries. Methods to Center Flexbox Content There are several approaches to center flexbox containers using JavaScript: Using justify-content: Centers items horizontally within the flex container by ...
Read MoreSmart concatenation of strings in JavaScript
We need to write a JavaScript function that concatenates two strings with a smart approach. If the last character of the first string matches the first character of the second string, we omit one of those duplicate characters to avoid redundancy. Problem Statement When concatenating strings like "Food" and "dog", a simple join would give "Fooddog". However, since both strings share the character 'd' at the junction, smart concatenation removes the duplicate to produce "Foodog". Example Here's how to implement smart string concatenation: const str1 = 'Food'; const str2 = 'dog'; const concatenateStrings ...
Read MoreGrouping array of array on the basis of elements in JavaScript
When working with arrays of arrays in JavaScript, you might need to group elements based on specific criteria. This article demonstrates how to group the second elements of subarrays that share the same first element. Problem Statement Suppose we have an array of arrays where each subarray contains exactly two elements: const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]]; console.log("Input array:", arr); Input array: [ [ 1, 45 ], [ 1, 34 ], [ 1, 49 ], [ 2, 34 ], [ 4, 78 ...
Read MoreHow to get id from tr tag and display it in a new td with JavaScript?
When working with HTML tables, you often need to extract the ID from table rows and display them within the table itself. This tutorial shows how to get the ID from tags and add them as new table cells using JavaScript. Sample Table Structure Consider the following table with ID attributes on each row: StudentName StudentCountryName ...
Read MorePrefix calculator using stack in JavaScript
A Reverse Polish Notation (RPN) calculator processes operators after their operands, using a stack data structure. This implementation evaluates mathematical expressions efficiently without parentheses. How RPN Works In RPN, operators follow their operands. For example, "3 4 +" means "3 + 4". The algorithm uses a stack: Push operands onto the stack When encountering an operator, pop two operands, perform the operation, and push the result back The final stack contains the result Step-by-Step Process Consider the input array: const arr = [1, 5, '+', 6, 3, '-', '/', 7, '*']; ...
Read MoreSmallest prime number just greater than the specified number in JavaScript
We are required to write a JavaScript function that takes in a positive integer as the first and the only argument. The function should find one such smallest prime number which is just greater than the number specified as argument. For example − If the input is − const num = 18; Then the output should be: const output = 19; Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a ...
Read MoreFinding the date at which money deposited equals a specific amount in JavaScript
When calculating compound interest over time, you may need to determine the exact date when your deposited money will reach a target amount. This problem involves daily compounding at a given annual interest rate. Problem Statement Given an initial deposit amount, a target amount, and an annual interest rate, we need to find the date when the deposited money (with daily compounding) will equal or exceed the target amount. The calculation starts from January 1st, 2021, with interest compounded daily at a rate of p percent divided by 360 days. Solution Approach The solution uses a ...
Read More