Web Development Articles

Page 471 of 801

Rearrange an array in maximum minimum form by JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

We are required to write a function, say minMax() that takes in an array of Numbers and rearranges the elements such that the greatest element appears first followed by the smallest elements then the second greatest element followed by second smallest element and so on. For example − // if the input array is: const input = [1, 2, 3, 4, 5, 6, 7] // then the output should be: const output = [7, 1, 6, 2, 5, 3, 4] So, let's write the complete code for this function − Approach: Using Two Pointers ...

Read More

Strictly increasing or decreasing array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 495 Views

In Mathematics, a strictly increasing function is one where values always increase, while a strictly decreasing function has values that always decrease. In JavaScript, we can check if an array follows either pattern. We need to write a function that takes an array of numbers and returns true if it's either strictly increasing or strictly decreasing, otherwise returns false. Understanding the Logic The key insight is to check if consecutive elements maintain the same slope (all increasing or all decreasing). We use a helper function sameSlope that compares three consecutive numbers to ensure they follow the same ...

Read More

How to check if every property on object is the same recursively in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 498 Views

When working with nested objects in JavaScript, you might need to check if all leaf values (final non-object values) are identical. This requires a recursive approach to traverse through all nested levels and compare the actual values at the end of each branch. For example, in this object: const obj = { a: 1, b: 1, c: { aa: 1 } }; The function should return true because all leaf values equal 1, even though one is ...

Read More

Finding persistence of number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 431 Views

The additive persistence of a number is the count of times you must repeatedly sum its digits until you get a single digit. This is a common programming problem that demonstrates recursion and digit manipulation. Understanding Additive Persistence For any positive integer, we replace it with the sum of its digits repeatedly until we reach a single digit (0-9). The number of iterations required is the additive persistence. For example, with 1679583: 1679583 → 1+6+7+9+5+8+3 = 39 (Pass 1) 39 → 3+9 = 12 ...

Read More

Parse array to equal intervals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

Let's say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so that it gets divided into n equal intervals. For example: // if the input array is const arr = [12, 48]; // and the interval is 4 //then the output array should be: const output = [12, 21, 30, 39, 48]; This way the array ...

Read More

Expressing numbers in expanded form - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 902 Views

Suppose we are given a number 124 and are required to write a function that takes this number as input and returns its expanded form as a string. The expanded form of 124 is − '100+20+4' How It Works The algorithm converts each digit to its place value by multiplying it with the appropriate power of 10, then joins non-zero values with '+' signs. Example Following is the code − const num = 125; const expandedForm = num => { const numStr = String(num); ...

Read More

Recursively flat an object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

We are required to write a function that flattens nested objects by converting deeply nested properties into dot-notation keys. This is useful for data transformation and API processing. If the input object is: const input = { a: 0, b: {x: {y: 1, z: 2}}, c: 3 }; Then the output of the function should be: const output = { a: 0, 'b.x.y': 1, 'b.x.z': 2, c: 3 } Recursive ...

Read More

Checking if two arrays can form a sequence - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

We are required to write a JavaScript function that takes in two arrays of numbers. And the function should return true if the two arrays upon combining and shuffling can form a consecutive sequence, false otherwise. For example − If the arrays are − const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; Then the output should be true because when combined and sorted, they form: [1, 2, 3, 4, 5, 6, 7, 8, 9] which is a consecutive sequence. Example Following is the code − ...

Read More

How do I write a function that takes an array of values and returns an object JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 624 Views

Let's say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types. For example − // if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) { 'string' => [ 'class', 'name' ], 'number' => [ 2, 6 ], 'object' => [ [ 7, 8, 9 ], { name: 'Michael' ...

Read More

Count the number of data types in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 296 Views

We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type. Let's say the following is our array: const arr = [23, 'df', undefined, null, 12, { name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8]; Understanding Data Types in JavaScript JavaScript's typeof operator returns string representations of data types. Note that arrays and null both return "object", which is a known quirk of JavaScript. Example ...

Read More
Showing 4701–4710 of 8,010 articles
« Prev 1 469 470 471 472 473 801 Next »
Advertisements