AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 302 of 840

Difference between product and sum of digits of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 336 Views

We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should first calculate the sum of the digits of the number and then their product. Finally, the function should return the absolute difference between the product and the sum. For example, if the input number is 12345: Sum of digits: 1 + 2 + 3 + 4 + 5 = 15 Product of digits: 1 × 2 × 3 × 4 × 5 = 120 Absolute difference: |120 - 15| = 105 Example ...

Read More

Index difference of tuples in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

Problem We are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument. Suppose two indices, i and j in the array which satisfy the following conditions − i < j, and arr[i] { let max = 0; const stack = [0]; ...

Read More

ASCII to hex and hex to ASCII converter class in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 975 Views

In this tutorial, we'll create a JavaScript class that converts between ASCII and hexadecimal formats. This is useful for encoding text data, debugging, or working with binary protocols. Problem Statement We need to write a JavaScript class with two member functions: toHex: Takes an ASCII string and returns its hexadecimal equivalent toASCII: Takes a hexadecimal string and returns its ASCII equivalent For example, the string 'this is a string' should convert to hex '74686973206973206120737472696e67' and back to the original ASCII. How It Works ASCII to hex ...

Read More

Finding place value of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 865 Views

In JavaScript, finding the place value of digits means extracting each digit's positional worth. For example, in the number 1234, the place values are [1000, 200, 30, 4]. We need to write a function that takes a positive integer and returns an array with the place values of all digits. Problem Example If the input number is: const num = 1234; Then the output should be: [1000, 200, 30, 4] Using Recursive Approach This problem is well-suited for recursion since we need to process each digit and calculate ...

Read More

Generating Random Prime Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 680 Views

We are required to write a JavaScript function that takes in two numbers specifying a range. Our function should return a random prime number that falls in that range. Algorithm Overview The solution uses the Sieve of Eratosthenes algorithm to find all prime numbers in the range, then randomly selects one. This approach ensures efficiency for finding multiple primes. Example The code for this will be − const range = [100, 1000]; const getPrimes = (min, max) => { const result = Array(max + 1) ...

Read More

Counting substrings of a string that contains only one distinct letter in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 257 Views

We are required to write a JavaScript function that takes in a string as the only argument. The task of our function is to count all the contiguous substrings in the input string that contains exactly one distinct letter. The function should then return the count of all such substrings. For example, if the input string is 'iiiji', then the output should be 8 because the desired substrings are: 'i', 'i', 'i', 'ii', 'ii', 'iii', 'j', and 'i'. How It Works The algorithm identifies consecutive groups of identical characters and counts all possible contiguous substrings within ...

Read More

Computing Ackerman number for inputs in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

The Ackermann Function is a classic example of a recursive function that grows extremely quickly. It's notable for being a total computable function that is not primitive recursive, making it an important concept in theoretical computer science. Problem Statement We need to write a JavaScript function that takes two non-negative integers, m and n, and returns the Ackermann number A(m, n) defined by the following mathematical definition: A(m, n) = n+1 if m=0 A(m, n) = A(m-1, 1) if m>0 and n=0 A(m, n) = A(m-1, A(m, n-1)) if m>0 and n>0 Implementation ...

Read More

Find specific key value in array of objects using JavaScript

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

When working with JavaScript objects containing arrays of nested objects, you often need to find which parent key contains an object with specific property values. This is common when dealing with product catalogs, user groups, or categorized data. Example Data Structure Consider this product catalog object where each category contains an array of products: const obj = { "LAPTOP": [{ "productId": "123" }], "DESKTOP": [{ "productId": "456" ...

Read More

Large to Small Sorting Algorithm of already sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 392 Views

Suppose we have an array of integers that is already sorted in the increasing order. We are required to write a JavaScript function that without using the inbuilt Array.prototype.sort() method sorts the array like the following − First number should be the maximum Second number should be the minimum Third number should be the 2nd ...

Read More

Validating push pop sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 441 Views

Validating push/pop sequences is a common stack problem where we need to verify if a given pop sequence could result from a specific push sequence on an initially empty stack. Problem Statement Given two arrays pushed and popped containing unique elements, determine if the pop sequence could be achieved from the push sequence using stack operations. const pushed = [1, 2, 3, 4, 5]; const popped = [4, 5, 3, 2, 1]; How It Works The algorithm simulates stack operations by maintaining a stack and two pointers. We push elements until the stack ...

Read More
Showing 3011–3020 of 8,392 articles
« Prev 1 300 301 302 303 304 840 Next »
Advertisements