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
Web Development Articles
Page 265 of 801
Finding the character with longest consecutive repetitions in a string and its length using JavaScript
We need to write a JavaScript function that finds the character with the longest consecutive repetitions in a string and returns both the character and its count as an array. Problem Given a string, we want to identify which character appears the most times consecutively and return an array containing the character and its consecutive count. Example Here's a solution that tracks consecutive characters and finds the maximum: const str = 'tdfdffddffsdsfffffsdsdsddddd'; const findConsecutiveCount = (str = '') => { if (!str) return ['', 0]; ...
Read MoreCounting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript
We are required to write a JavaScript function that takes in an array of strings of English lowercase alphabets. Our function should map the input array to an array whose corresponding elements are the count of the number of characters that had the same 1-based index in the string as their 1-based index in the alphabets. For instance, this count for the string 'akcle' will be 3 because the characters 'a', 'c' and 'e' have 1-based index of 1, 3 and 5 respectively both in the string and the English alphabets. Understanding the Problem We need ...
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 MoreEvaluating a mathematical expression considering Operator Precedence in JavaScript
This article demonstrates how to evaluate mathematical expressions in JavaScript while respecting operator precedence. We'll implement a parser that handles the four basic arithmetic operators (+, -, *, /) and follows the standard order of operations. Problem Statement We need to write a JavaScript function that takes a mathematical expression as a string and returns its numerical result. The function must support: Addition (+) Subtraction (-) Multiplication (*) Division (/) as floating-point division The key challenge is handling operator precedence: multiplication and division must be evaluated ...
Read MoreCalculating variance for an array of numbers in JavaScript
Problem We need to write a JavaScript function that calculates the variance for an array of numbers. Variance measures how spread out the numbers are from their average (mean). The variance formula involves two steps: Mean (M) = (Sum of all numbers) / (Count of numbers) Variance (V) = (Sum of squared differences from mean) / (Count of numbers) Understanding Variance Variance tells us how much the numbers in our dataset differ from the average. A low variance means numbers are close to the mean, while high variance means they're spread out. ...
Read MoreSorting binary string having an even decimal value using JavaScript
We are required to write a JavaScript function that takes in a string containing binary strings of length 3, all separated by spaces. Our function should sort only the even numbers (when converted from binary to decimal) in ascending order, while leaving all odd numbers in their original positions. Problem Given a string with binary numbers, we need to: Convert each binary string to its decimal equivalent Identify which numbers are even Sort only the even numbers in ascending order Keep odd numbers in their original positions Example Let's look at the binary strings ...
Read MoreCalculating and adding the parity bit to a binary using JavaScript
A parity bit, or check bit, is a bit added to a string of bits to ensure that the total number of 1-bits in the string is even or odd. This is commonly used in error detection and data transmission. Problem Statement We need to write a JavaScript function that takes two parameters: the desired parity (either 'even' or 'odd') and a binary string representation. The function should return the parity bit (0 or 1) that needs to be added to achieve the specified parity. How Parity Bits Work Even Parity Example: ...
Read MoreImplementing a custom function like Array.prototype.filter() function in JavaScript
In JavaScript, the Array.prototype.filter() method creates a new array with elements that pass a test function. Let's implement a custom version to understand how it works internally. Problem We need to create a custom function that mimics Array.prototype.filter(). The function should: Live on the Array prototype Accept a callback function as an argument Call the callback for each array element with the element and its index Include elements in the result array only when the callback returns true Implementation Here's how we can implement our custom filter function: const arr = ...
Read MoreCounting number of triangle sides in an array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. The task of our function is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. For example, if the input to the function is − const arr = [2, 2, 3, 4]; Then the output should be − const output = 3; Triangle Validity Rule For three sides to ...
Read MoreCommon element with least index sum in JavaScript
We need to find common elements between two arrays that have the smallest sum of their indices from both arrays. When multiple elements tie for the smallest sum, we return all of them. Problem Understanding Given two arrays, for each common element, we calculate the sum of its index in the first array and its index in the second array. The element(s) with the minimum index sum are returned. For example, with arrays: const arr1 = ['a', 'b', 'c', 'd']; const arr2 = ['d', 'a', 'c']; Common elements and their index sums: ...
Read More