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
Front End Technology Articles
Page 188 of 652
Accumulating some value over using a callback function and initial value in JavaScript
We need to write a JavaScript function that takes an array, a callback function, and an initial value to accumulate values over array iteration, similar to Array.prototype.reduce(). Problem The goal is to create a custom reduce function that processes each array element through a callback function, accumulating a result from an initial value. Understanding Array Reduce The reduce method applies a callback function to each array element, building up a single result value. The callback receives the accumulator (previous result) and current element as parameters. Custom Reduce Implementation const arr = [1, 2, ...
Read MoreIterate over an object and remove false property in JavaScript
In JavaScript, you may need to recursively iterate through an object and remove properties with falsy values. This is common when cleaning up configuration objects or filtering data structures. The Problem Consider an object with nested structures containing "enabled" properties. We want to remove any objects where enabled is false, and also clean up any empty parent objects left behind. const obj = { a: { someKey: { propOne: '', ...
Read MoreGrouping array values in JavaScript
Suppose we have an array of objects containing some years and weeks data like this: const arr = [ {year: 2017, week: 45}, {year: 2017, week: 46}, {year: 2017, week: 47}, {year: 2017, week: 48}, {year: 2017, week: 50}, {year: 2017, week: 52}, {year: 2018, week: 1}, {year: 2018, week: 2}, {year: 2018, week: 5} ]; We are required to ...
Read MoreReturning the expanded form of a number in JavaScript
The expanded form of a number breaks down each digit into its place value. For example, 1234 becomes "1000 + 200 + 30 + 4". This is useful for teaching place value concepts and number decomposition. Problem We need to write a JavaScript function that takes a number and returns a string showing the expanded form, indicating the place value of each non-zero digit. Example const num = 56577; const expandedForm = (num = 0) => { const str = String(num); let res = ''; ...
Read MorePushing positives and negatives to separate arrays in JavaScript
We are required to write a function that takes in an array and returns an object with two arrays positive and negative. They both should be containing all positive and negative items respectively from the array. We will be using the Array.prototype.reduce() method to pick desired elements and put them into an object of two arrays. Using Array.reduce() Method The code for this will be − const arr = [97, -108, 13, -12, 133, -887, 32, -15, 33, -77]; const splitArray = (arr) => { return arr.reduce((acc, val) => { ...
Read MoreCurrified function that multiples array elements in JavaScript
Problem We need to write a JavaScript function that takes an array and returns another function. This returned function takes a number and produces a new array where each element is the product of the corresponding element from the original array and the number. What is a Curried Function? A curried function breaks down a function that takes multiple arguments into a series of functions that each take a single argument. In this case, instead of multiply(array, number), we have multiply(array)(number). Example Following is the code − const arr = [2, 5, 2, 7, 8, 4]; ...
Read MoreSum all duplicate values in array in JavaScript
We need to write a JavaScript function that takes an array of numbers with duplicate entries and sums all duplicate values. Each unique number appears once in the result, multiplied by its frequency count. Problem Understanding For array [1, 3, 1, 3, 5, 7, 5, 3, 4]: 1 appears 2 times → 1 × 2 = 2 3 appears 3 times → 3 × 3 = 9 5 appears 2 times → 5 × 2 = 10 7 appears 1 time → 7 × 1 = 7 4 appears 1 time → 4 × 1 = 4 ...
Read MoreReturning number with increasing digits. in JavaScript
We are required to write a JavaScript function that takes in a number n. Our function should return it with its digits in descending order. Essentially, we should rearrange the digits to create the highest possible number. Problem Given a number, we need to arrange its digits in descending order to form the largest possible number from those digits. Solution The approach is to convert the number to a string, split it into individual digits, sort them in descending order, and then convert back to a number. Example const num = 5423267; ...
Read MoreFinding deviations in two Number arrays in JavaScript
We are required to write a JavaScript function that takes in two number arrays and returns the elements that are not common to both arrays (symmetric difference). For example, if the two arrays are: const arr1 = [2, 4, 2, 4, 6, 4, 3]; const arr2 = [4, 2, 5, 12, 4, 1, 3, 34]; Then the output should be: [6, 5, 12, 1, 34] Method 1: Using indexOf() Method This approach uses nested loops to check if elements exist in the other array: const arr1 = [2, ...
Read MoreDeep count of elements of an array using JavaScript
We are required to write a JavaScript function that takes in a nested array of elements and returns the deep count of all elements present in the array, including those in nested subarrays. Problem Given a nested array, we need to count all elements at every level of nesting. Input const arr = [1, 2, [3, 4, [5]]]; Output const output = 7; Because the elements at level 1 are 2, elements at level 2 are 2 and elements at level 3 are 1. Hence the deep count is 7. Using Recursive Reduce Method ...
Read More