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 283 of 840
Finding median for every window in JavaScript
In mathematics, the median is the middle value in an ordered (sorted) list of numbers. If the list has an even number of elements, the median is the average of the two middle values. Problem Statement We need to write a JavaScript function that takes an array of integers and a window size, then calculates the median for each sliding window of that size. The function returns an array containing all the calculated medians. For example, given: const arr = [5, 3, 7, 5, 3, 1, 8, 9, 2, 4, 6, 8]; const windowSize = ...
Read MoreHow to convert a string with zeros to number in JavaScript?
When you have a string containing numbers with dots or zeros as separators, you can convert it to a number using JavaScript's built-in methods. This is commonly needed when processing formatted numeric data. The Problem Consider a string like "453.000.00.00.0000" where dots are used as thousand separators rather than decimal points. Direct conversion methods like Number() won't work correctly because JavaScript interprets the first dot as a decimal separator. let stringValue = "453.000.00.00.0000"; console.log("Original string:", stringValue); console.log("Direct Number() conversion:", Number(stringValue)); // NaN Original string: 453.000.00.00.0000 Direct Number() conversion: NaN Using parseInt() ...
Read MoreFiltering array to contain palindrome elements in JavaScript
We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. For example If the input array is − const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be − const output = [12321, 'did']; We will create a helper function that takes in a number or a string and checks if it's a palindrome or not. Then we will loop over the array, filter ...
Read MoreCreating an array of objects based on another array of objects JavaScript
In JavaScript, you can create a new array of objects based on an existing array by transforming each object's structure. This is commonly done using the map() method to extract specific properties and create new object formats. Suppose we have an array of objects containing user data like this: const arr = [ {"user":"dan", "liked":"yes", "age":"22"}, {"user":"sarah", "liked":"no", "age":"21"}, {"user":"john", "liked":"yes", "age":"23"}, ]; console.log("Original array:", arr); Original array: [ { user: 'dan', liked: 'yes', age: '22' }, { ...
Read MoreBuild tree array from flat array in JavaScript
Converting a flat array into a hierarchical tree structure is a common task when working with JSON data. This process transforms an array where each item has an id and parentId into a nested structure with parent-child relationships. Every entry in the JSON array has: id — a unique identifier parentId — the id of the parent node (which is "0" for root nodes) level — the depth level in the tree hierarchy The JSON data is already ordered, meaning each entry will ...
Read MoreForming string using 0 and 1 in JavaScript
We need to write a JavaScript function that takes an array of binary strings and determines the maximum number of strings that can be formed using at most m zeros and n ones. Problem Statement Given an array of strings containing only '0' and '1', find how many strings can be selected such that the total count of zeros doesn't exceed m and the total count of ones doesn't exceed n. For example, with the array ["10", "0001", "111001", "1", "0"] and limits m=5 zeros and n=3 ones: Input: arr = ["10", "0001", "111001", "1", ...
Read MoreHow to make filter and word replacement method - JavaScript?
In JavaScript, there's no built-in method to replace all occurrences of a word in a string. You can create custom functions using methods like split() and join(), or use modern approaches like replaceAll(). Let's explore different methods to filter and replace words in a string: var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; Method 1: Using split() and join() This method splits the string by the target word and joins the parts with the replacement: var sentence = "Yes, My Name ...
Read MoreHow to read JSON file in JavaScript?
There are three effective methods to read JSON files in JavaScript using fetch(), import statements, and require(). This article covers practical examples for both browser and Node.js environments. JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write. In this article, we'll explore three different approaches to reading JSON files in JavaScript: Approaches to Read JSON file Using the fetch() method (Browser) Using the import statement (ES6 Modules) Using the require() method (Node.js) Each method ...
Read MoreEncoding string to reduce its size in JavaScript
Problem We need to write a JavaScript function that takes a string and encodes it using a compression format. The function should compare the encoded string with the original and return whichever is smaller. The encoding rule is: n[s], where s inside the square brackets is being repeated exactly n times. For example, "ddd" can be encoded to "3[d]", but since "3[d]" has 4 characters while "ddd" has only 3, the function should return "ddd". Example Input and Output For the input string: 'aabcaabcd' The expected ...
Read MoreSplitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?
When working with hyphen-delimited strings containing negative numbers or ranges, simple split('-') won't work correctly because it treats every hyphen as a delimiter. We need regular expressions to distinguish between separating hyphens and negative number signs. The Problem Consider these strings with mixed content: var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; Using split('-') would incorrectly split negative numbers and ranges, giving unwanted empty strings and broken values. Solution Using Regular Expression We use a lookahead pattern to split only at hyphens that precede letters or number ranges: var firstValue ...
Read More