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
Articles by AmitDiwan
Page 297 of 840
Pushing false objects to bottom in JavaScript
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 MoreMultiply and Sum Two Arrays in JavaScript
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 MoreCalculating the sum of digits of factorial JavaScript
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 MoreFinding the first non-repeating character of a string in JavaScript
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 MoreProblem: Time taken by tomatoes to rot in JavaScript
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 MoreEncoding string based on character frequency in JavaScript
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 MoreHow to find all partitions of a multiset, where each part has distinct elements in JavaScript
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 MoreSorting numbers in descending order but with `0`s at the start JavaScript
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 MoreParts of array with n different elements in JavaScript
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 MoreArray index to balance sums in JavaScript
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