AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 409 of 840

Left right subarray sum product - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

We are required to write a JavaScript function that takes in an array of numbers of length N (N should be even) and divides the array into two sub-arrays (left and right) containing N/2 elements each, calculates the sum of each sub-array, and then multiplies both sums together. For example: If the input array is: const arr = [1, 2, 3, 4] The calculation would be: Left subarray: [1, 2] → sum = 1 + 2 = 3 Right subarray: [3, 4] → sum = 3 + 4 = 7 Product: 3 × ...

Read More

JavaScript Numbers example

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 197 Views

JavaScript has a single number type that represents both integers and floating-point numbers. Unlike other languages, JavaScript doesn't distinguish between different numeric types. Number Types in JavaScript All numbers in JavaScript are stored as 64-bit floating-point values, following the IEEE 754 standard. This means you can work with both whole numbers and decimals seamlessly. JavaScript Numbers body { ...

Read More

Sort array based on another array in JavaScript

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

We often need to sort an array based on the order specified in another array. This technique is useful when you want certain elements to appear first while maintaining the original order of remaining elements. For example, we have an original array and want elements from a sortOrder array to appear at the beginning: const originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van']; Using Custom Sort Function We can create a custom comparator function that prioritizes elements present in the sortOrder array: const originalArray = ['Apple', 'Cat', ...

Read More

Rearrange string so that same character become n distance apart JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 326 Views

We need to rearrange a string so that identical characters are exactly n positions apart from each other. This problem requires careful character placement to maintain the specified distance constraint. For example, if we have the string "accessories" and n = 3, we need to ensure that each 's' character is exactly 3 positions away from other 's' characters, and the same applies to other repeated characters. Problem Understanding The algorithm works by: Counting the frequency of each character Sorting characters by frequency (most frequent first) Placing characters in a round-robin fashion with n distance apart ...

Read More

How to access nested JSON property based on another property's value in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 578 Views

To access a nested JSON property based on another property's value in JavaScript, you can loop through the data and match the target property. This technique is useful when searching for specific records in JSON arrays. Example: Finding Student Marks by Subject var actualJSONData = JSON.parse(studentDetails()); var studentMarks = getMarksUsingSubjectName(actualJSONData, "JavaScript"); console.log("The student marks = " + studentMarks); function getMarksUsingSubjectName(actualJSONData, givenSubjectName) { for (var tempObj of actualJSONData) { if (tempObj.subjectName === givenSubjectName) { ...

Read More

Adjacent elements of array whose sum is closest to 0 - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray of two adjacent elements from the original array whose sum is closest to 0. If the length of the array is less than 2, we should return the whole array. Problem Statement For example: If the input array is − const arr = [4, 4, 12, 3, 3, 1, 5, -4, 2, 2]; Here, the sum of adjacent pair [5, -4] is 1 which is closest to 0 for any two adjacent elements of ...

Read More

Lambdas with Arrow Functions in JavaScriptLambdas with Arrow Functions in JavaScript

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

Lambda functions in JavaScript are small anonymous functions that can take parameters and return values. Arrow functions provide a concise way to write lambda functions and are commonly used for functional programming operations like map(), filter(), and reduce(). Syntax // Single parameter (parentheses optional) const lambda1 = x => x * 2; // Multiple parameters const lambda2 = (x, y) => x + y; // No parameters const lambda3 = () => "Hello World"; // Multiple statements (requires curly braces and return) const lambda4 = x => { const result ...

Read More

Filter an object based on an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 836 Views

Let's say we have an array and an object like this: const arr = ['a', 'd', 'f']; const obj = { "a": 5, "b": 8, "c": 4, "d": 1, "e": 9, "f": 2, "g": 7 }; We are required to write a function that takes in the object and the array and filter away all the object properties that are not an element of the array. So, the output should only contain 3 properties, namely: "a", "d" and "f". Method 1: ...

Read More

Object difference in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

In JavaScript, finding the difference between two objects means identifying keys that exist in the first object but not in the second. This is useful for comparing data structures, tracking changes, or filtering object properties. Problem Statement We need to write a JavaScript function that takes two objects (possibly nested) and returns a new object containing only the key-value pairs that exist in the first object but are missing from the second object. Basic Implementation Here's a function that compares two objects and returns the difference: const obj1 = { "firstName": ...

Read More

How to know if two arrays have the same values in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 405 Views

In JavaScript, comparing arrays directly with == or === compares references, not values. To check if two arrays contain the same values (regardless of order), we need custom comparison logic. The Problem with Direct Comparison var firstArray = [100, 200, 400]; var secondArray = [400, 100, 200]; console.log(firstArray === secondArray); // false console.log([1, 2] === [1, 2]); // false - different objects false false Using Sort and Compare Method The most reliable approach is to sort both arrays and compare ...

Read More
Showing 4081–4090 of 8,392 articles
« Prev 1 407 408 409 410 411 840 Next »
Advertisements