AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 314 of 840

Calculating quarterly and yearly average through JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 Views

In this tutorial, we'll learn how to calculate quarterly and yearly averages from an array of numbers using JavaScript. This involves chunking data into groups and computing their averages. Suppose we have an array of numbers like this: const arr = [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log("Original array:", arr); Original array: [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] We need to group this array ...

Read More

Filtering array within a limit JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 793 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and an upper limit and lower limit number as second and third argument respectively. Our function should filter the array and return a new array that contains elements between the range specified by the upper and lower limit (including the limits). Syntax const filterByLimits = (arr, upper, lower) => { return arr.filter(element => element >= lower && element { let res = []; res = arr.filter(el => { return el >= lower && el { return arr.filter(element => element >= lower && element = lower && element

Read More

Calculating the weight of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 949 Views

In JavaScript, calculating the weight of a string means finding the sum of alphabetical positions of all characters. Each letter's weight is its position in the alphabet (a=1, b=2, c=3, etc.). Understanding Character Weight The weight of an English alphabet character is its 1-based index position: 'a' has weight 1 'b' has weight 2 'c' has weight 3 'z' has weight 26 Method 1: Using indexOf() This approach uses a reference string to find each character's position: const str = 'this is a string'; const calculateWeight = (str = '') ...

Read More

Deleting desired node from a Binary Search Tree in JavaScrip

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 332 Views

Deleting a node from a Binary Search Tree is more complex than insertion because we need to handle three different cases depending on the node's children. This operation maintains the BST property where left children are smaller and right children are larger. Problem We have a Binary Search Tree implementation with insertion functionality. We need to add a deleteNode() function that removes a node with a specific value while maintaining the BST structure. class Node { constructor(data) { this.data = data; ...

Read More

Moving all vowels to the end of string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 397 Views

In JavaScript, you can move all vowels to the end of a string while maintaining the relative positions of consonants. This is useful for text processing and string manipulation tasks. Problem We need to write a JavaScript function that takes a string and constructs a new string where all consonants maintain their relative positions and all vowels are moved to the end. Approach The solution involves iterating through the string, separating vowels and consonants into different variables, then concatenating them with consonants first and vowels at the end. Example const str = 'sample ...

Read More

Finding middlemost node of a linked list in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

In JavaScript, finding the middle node of a linked list is a common problem that can be efficiently solved using the two-pointer technique, also known as the "tortoise and hare" algorithm. Problem Statement We need to write a JavaScript function that takes the head of a linked list and returns the value of the middlemost node. If there are two middle nodes (even number of nodes), we return the second one. For example, given the list: [4, 6, 8, 9, 1], the middle node contains the value 8. Two-Pointer Approach The most efficient solution uses ...

Read More

Group strings starting with similar number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

In JavaScript, you can group strings that start with the same number by extracting the first part before the decimal and comparing consecutive elements. This is useful when working with version numbers, decimal classifications, or any structured string data. Problem Statement Given an array of number strings like this: const arr = ["1.1", "1.2", "1.3", "2.1", "2.2", "3.1", "3.2", "3.3", "4.1", "4.2"]; console.log("Input array:", arr); Input array: [ '1.1', '1.2', '1.3', '2.1', '2.2', '3.1', '3.2', '3.3', '4.1', '4.2' ] We need to group all strings starting with the same number into ...

Read More

Convert 2d tabular data entries into an array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

Converting 2D tabular data into an array of objects is a common requirement when working with datasets. This transformation groups rows by a unique identifier and creates objects with dynamic properties. Problem Overview Suppose we have an array of arrays representing tabular data: const arr = [ ["Ashley", "2017-01-10", 80], ["Ashley", "2017-02-10", 75], ["Ashley", "2017-03-10", 85], ["Clara", "2017-01-10", 90], ["Clara", "2017-02-10", 82] ]; We need to transform this into an array of objects where each unique name becomes a ...

Read More

Determining beautiful number string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 414 Views

A numeric string is called a beautiful string if it can be split into a sequence of two or more positive integers, satisfying the following conditions: arr[i] - arr[i - 1] = 1, for any i in the sequence, i.e., each element is exactly one more than the previous element. No element should contain a leading zero. For example, '50607' can be split into [5, 06, 07], but it's not beautiful because 06 and 07 have leading zeros. The sequence order cannot be rearranged. Example For ...

Read More

Finding Fibonacci sequence in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 672 Views

In JavaScript, finding the longest Fibonacci subsequence in an array involves identifying a sequence where each element is the sum of the two preceding ones. This is a dynamic programming problem that requires careful tracking of potential Fibonacci sequences. Fibonacci Sequence Definition A sequence X₁, X₂, ..., Xₙ is Fibonacci if: n ≥ 3 (at least 3 elements) Xᵢ + Xᵢ₊₁ = Xᵢ₊₂ for all valid i (each element equals sum of previous two) Problem Statement Given an array of numbers, find the length of ...

Read More
Showing 3131–3140 of 8,392 articles
« Prev 1 312 313 314 315 316 840 Next »
Advertisements