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
Web Development Articles
Page 189 of 801
Finding sum of all numbers within a range in JavaScript
Problem We are required to write a JavaScript function that takes in an array that specifies a range. Our function should find and return the sum of all the natural numbers falling in the range including the range numbers. Example Following is the code − const range = [4, 67]; const findSum = ([l, h]) => { let sum = 0; for(let i = l; i { // Formula: Sum = n * (first + last) / 2 // where ...
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 MoreBuilding a lexicographically increasing sequence of first n natural numbers in JavaScript
We are required to write a JavaScript function that takes in a number n and returns an array containing the first n natural numbers. The condition is that the numbers should be sorted lexicographically, which means all numbers starting with 1 should come before any starting with 2, 3, 4, and so on. Understanding Lexicographical Ordering Lexicographical ordering treats numbers as strings. For example, "10" comes before "2" because "1" comes before "2" alphabetically. This creates the sequence: 1, 10, 11, 12, ..., 19, 2, 20, 21, ..., etc. Example Implementation const num = 24; ...
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 MoreAccumulating 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 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 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 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 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 More