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
Javascript Articles
Page 167 of 534
Evaluating 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 MoreMerging and rectifying arrays in JavaScript
We need to create a JavaScript function that merges two arrays and removes duplicates, ensuring each element appears only once in the final result. Problem We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second arguments. Our function should merge the elements of both these arrays into a new array and if upon merging or before merging there exists any duplicates, we should delete the excess duplicates so that only one copy of each element is present in the merged array. The ...
Read MoreFiltering out numerals from string in JavaScript
We are required to write a JavaScript function that takes in a string, str, which contains a combination of alphabets, special characters and numbers. Our function should return a new string based on the input string that contains only the numbers present in the string str, maintaining their relative order. For example, if the input to the function is: const str = 'revd1fdfdfs2v34fd5gfgfd6gffg7ds'; Then the output should be: '1234567' Using for Loop and Type Coercion This approach iterates through each character and uses the unary plus operator (+) to ...
Read MoreLongest subarray with unit difference in JavaScript
We need to find the length of the longest subarray where the difference between maximum and minimum values is exactly 1. This means the subarray can only contain two consecutive numbers (like 3 and 4) or just one unique number repeated. Problem Statement Given an array of numbers, find the length of the longest subarray where the difference between its maximum and minimum values is exactly 1. For example, with input array: const arr = [2, 4, 3, 3, 6, 3, 4, 8]; The longest valid subarray is [4, 3, 3, 3, 4] ...
Read More