AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 297 of 840

Pushing false objects to bottom in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

Suppose we have an array of objects like this − const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ]; We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property. Understanding Falsy Values In JavaScript, falsy values include: false, ...

Read More

Multiply and Sum Two Arrays in JavaScript

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

We are required to write a JavaScript function that takes in two arrays of equal length. The function should multiply the corresponding (by index) values in each, and sum the results. For example: If the input arrays are − const arr1 = [2, 3, 4, 5]; const arr2 = [4, 3, 3, 1]; then the output should be 34, because: (2*4 + 3*3 + 4*3 + 5*1) = 8 + 9 + 12 + 5 = 34 Using for Loop The most straightforward approach uses a for loop to iterate ...

Read More

Calculating the sum of digits of factorial JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 556 Views

We are required to write a JavaScript function that takes in a number. The function should first calculate the factorial of that number and then it should return the sum of the digits of the calculated factorial. For example, for the number 6, the factorial will be 720, so the sum of digits (7 + 2 + 0) should be 9. Understanding the Problem This problem involves two steps: Calculate the factorial of a given number Sum all digits in the factorial result Step 1: Calculate ...

Read More

Finding the first non-repeating character of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 565 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should find and return the index of first character it encounters in the string which appears only once in the string. If the string does not contain any unique character, the function should return -1. For example, if the input string is: const str = 'hellohe'; Then the output should be: const output = 4; Because the character 'o' at index 4 is the first character that ...

Read More

Problem: Time taken by tomatoes to rot in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

Problem We are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument. The numbers in the array can be: the value 0 which represents an empty cell; the value 1 which represents a fresh tomato; the value ...

Read More

Encoding string based on character frequency in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 272 Views

We are required to write a JavaScript function that takes in a string as an argument and creates a new string based on character frequency. Each character in the new string is '(' if that character appears only once in the original string, or ')' if that character appears more than once. The comparison should be case-insensitive. Problem Statement Given a string, encode each character as: '(' - if the character appears only once ')' - if the character appears more than once Ignore case when counting character frequency For example, if the input is ...

Read More

How to find all partitions of a multiset, where each part has distinct elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

Finding all partitions of a multiset where each part contains distinct elements is a complex combinatorial problem. We need to create an algorithm that generates all possible ways to divide elements into groups without repetition within each group. Let's say we have an array with repeated elements: const arr = [A, A, B, B, C, C, D, E]; We need to find all combinations that use the entire array, where no elements are repeated within each partition. Example Partitions [A, B, C, D, E] [A, B, C] [A, B, C, D] [A, ...

Read More

Sorting numbers in descending order but with `0`s at the start JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 544 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array of numbers on the following criteria: If the array contains any zeros, they should all appear in the beginning. All the remaining numbers should be placed in a decreasing order. For example, if the input array is: const arr = [4, 7, 0, 3, 5, 1, 0]; Then after applying the sort, the array should become: const output = [0, 0, 7, 5, ...

Read More

Parts of array with n different elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 228 Views

We are required to write a JavaScript function that takes in an array of literals, arr, as the first argument. The second argument to our function will be a number, num. Our function should count and return the number of subarrays of the array that contains exactly num distinct elements. Problem Statement Given an array and a number, find all subarrays that contain exactly that many distinct elements. For example, if the input to the function is: const arr = [12, 15, 12, 15, 18]; const num = 2; Then the output should ...

Read More

Array index to balance sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 630 Views

Finding a balance point in an array means locating an index where the sum of elements on the left equals the sum of elements on the right. This is a common array manipulation problem in JavaScript. Problem Statement We need to write a JavaScript function that takes an array of integers and returns the index where the left sum equals the right sum. If no such index exists, return -1. For example, with the array [1, 2, 3, 4, 3, 2, 1], index 3 is the balance point because: Left side (indices 0-2): 1 + ...

Read More
Showing 2961–2970 of 8,392 articles
« Prev 1 295 296 297 298 299 840 Next »
Advertisements