AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 293 of 840

Evaluating a string as a mathematical expression in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

We are required to write a JavaScript function that takes in a stringified mathematical equation. The function should return the result of the equation provided to the function. For example: If the equation is − const str = '1+23+4+5-30'; Then the output should be 3 Example The code for this will be − const str = '1+23+4+5-30'; const compute = (str = '') => { let total = 0; str = str.match(/[+\-]*(\.\d+|\d+(\.\d+)?)/g) || []; while (str.length) { ...

Read More

Sum array of rational numbers and returning the result in simplest form in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 225 Views

We are required to write a JavaScript function that takes in an array of exactly two subarrays with two numbers each. Both the subarrays represent a rational number in fractional form. Our function should add the rational numbers and return a new array of two numbers representing the simplest form of the added rational number. Problem When adding two fractions like 1/2 + 1/3, we need to: Find a common denominator Add the numerators Simplify the result using the Greatest Common Factor (GCD) Solution The mathematical formula for adding fractions is: a/b + ...

Read More

Breaking camelCase syntax in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 Views

We need to write a JavaScript function that takes a camelCase string and converts it into a readable format by adding spaces before uppercase letters. Our function should construct and return a new string that splits the input string using a space between words. Problem Example For example, if the input to the function is: Input const str = 'thisIsACamelCasedString'; Expected Output 'this Is A Camel Cased String' Solution Using String Iteration The approach iterates through each character and adds a space before uppercase letters (except the first character): ...

Read More

JavaScript: Combine highest key values of multiple arrays into a single array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 280 Views

We are required to write a JavaScript function that takes in any number of arrays of numbers. Our function should return an array of greatest numbers picked from the corresponding positions of the input arrays. The number of elements in the output array should be equal to the length of the longest input array. Problem Understanding Given multiple arrays, we need to compare elements at the same index positions and select the maximum value from each position to form a new array. Example The code for this will be: const arr1 = [117, 121, ...

Read More

JavaScript - summing numbers from strings nested in array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

When working with arrays containing string numbers (like credit card numbers), you often need to extract and sum the numeric values. This article shows how to find the string with the greatest sum of digits. Problem Statement Given an array of credit card numbers as strings, we need to find the card with the highest sum of digits. If multiple cards have the same sum, return the last one encountered. const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']; Solution Approach The solution involves two main steps: extracting numbers from each string and calculating their ...

Read More

Array thirds with equal sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 184 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. Our function should return true if and only if we can partition the array into three non-empty parts with equal sums, false otherwise. Problem Statement Given an array of integers, determine if it can be split into exactly three contiguous subarrays with equal sums. For example, if the input to the function is: const arr = [3, 3, 6, 5, -2, 2, 5, 1, -9, 4]; Then the output should be: ...

Read More

Decrypting source message from a code based on some algorithm in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 221 Views

We need to write a JavaScript function that decrypts a message by reversing an encryption algorithm. The encryption process involves reversing the string and replacing letters with their ASCII codes in quotes. Understanding the Encryption Algorithm The original encryption algorithm works as follows: Reverse the message string Replace every letter with its ASCII code in quotes (A becomes '65', h becomes '104') Keep digits and spaces unchanged The Decryption Function To decrypt, we need to reverse this process by applying the same algorithm to the encrypted message: const str = '12 hello ...

Read More

Finding the final direction of movement in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 404 Views

Problem We need to write a JavaScript function that takes an array of directional characters and finds the final direction after canceling out opposite movements. The array contains only 4 possible characters: 'N' → stands for North direction 'S' → stands for South direction 'W' → stands for West direction 'E' → stands for East direction Each character represents a unit move in that direction. When opposite directions ('S' and 'N') or ('E' and 'W') appear adjacently, they cancel each other out. Our ...

Read More

All combinations of sums for array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 920 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n as the second argument. The number n will always be less than or equal to the length of the array. Our function should return an array of the sum of all elements of all the possible subarrays of length n from the original array. For example, If the input is: const arr = [2, 6, 4]; const n = 2; Then the output should be: const output = ...

Read More

JavaScript - Find keys for the matched values as like query in SQL

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 460 Views

In JavaScript, you can filter object properties based on value matching, similar to SQL's WHERE clause with LIKE operator. This is useful for searching through data structures and finding keys that match specific criteria. Problem Statement Given an object with key-value pairs, we need to find all keys whose values contain a specific search term (case-insensitive). const obj = { "100": "Jaipur", "101": "Delhi", "102": "Raipur", "104": "Goa" }; We want to create a function that returns ...

Read More
Showing 2921–2930 of 8,392 articles
« Prev 1 291 292 293 294 295 840 Next »
Advertisements