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 409 of 840
Left right subarray sum product - JavaScript
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 MoreJavaScript Numbers example
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 MoreSort array based on another array in JavaScript
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 MoreRearrange string so that same character become n distance apart JavaScript
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 MoreHow to access nested JSON property based on another property's value in JavaScript?
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 MoreAdjacent elements of array whose sum is closest to 0 - JavaScript
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 MoreLambdas with Arrow Functions in JavaScriptLambdas with Arrow Functions in JavaScript
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 MoreFilter an object based on an array JavaScript
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 MoreObject difference in JavaScript
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 MoreHow to know if two arrays have the same values in JavaScript?
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