AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 385 of 840

Finding the Largest Triple Product Array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. Based on the array taken in as input, the function should construct a new array of the same length based on the following criteria. Any corresponding element of the output array should be the product of the three largest numbers encountered thus far. If the corresponding index is less than 3 (we have not encountered three elements yet) then the corresponding value should be -1. Although we can use non-unique values to calculate the product, those non-unique values should ...

Read More

Finding astrological signs based on birthdates using JavaScript

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

We are required to write a JavaScript function that takes in a date object and returns the astrological sign related to that birthdate based on zodiac date ranges. Understanding Zodiac Signs Each zodiac sign corresponds to specific date ranges throughout the year. The challenge is handling the transition dates correctly, especially for signs that span across months. Example Following is the code: const date = new Date(); // as on 2 April 2021 const findSign = (date) => { const days = [21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22]; const signs = ["Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn"]; let month = date.getMonth(); let day = date.getDate(); if(month == 0 && day

Read More

Length of shortest unsorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 232 Views

We are required to write a JavaScript function that takes in an array of numbers and finds the length of the shortest continuous subarray that, when sorted, makes the entire array sorted in ascending order. Our function needs to find the length of one continuous subarray such that if we only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. Problem Example For example, if the input array is: const arr = [3, 7, 5, 9, 11, 10, 16]; console.log("Original array:", arr); Original array: ...

Read More

Fetch specific values from array of objects in JavaScript?

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

Fetching specific values from an array of objects is a common JavaScript task. This article demonstrates multiple approaches to filter and extract data based on specific criteria. Sample Data Let's start with an array of employee objects: const details = [ { employeeFirstName: "John", employeeLastName: "Doe" }, { employeeFirstName: "David", employeeLastName: "Miller" ...

Read More

Filtering out the non-unique value to appear only once in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

We have an array that contains some duplicate values appearing multiple times. We need to extract only the elements that appear more than once in the array, but show each duplicate element only once in the result. const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; console.log("Original array:", arr); Original array: [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4] We need to write a JavaScript function that filters out elements that appear multiple times and returns each duplicate element only once. For ...

Read More

Merge JSON array date based JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 802 Views

When working with JSON arrays containing objects with date properties, you often need to merge objects that share the same date. This is common when combining data from different sources or consolidating time-series data. Suppose we have the following array of objects: const arr = [ { "date": "2010-01-01", "price": 30 }, { "date": "2010-02-01", ...

Read More

Find what numbers were pressed to get the word (opposite of phone number digit problem) in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

The mapping of the numerals to alphabets in the old keypad type phones used to be like this: const mapping = { 1: [], 2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g', 'h', 'i'], 5: ['j', 'k', 'l'], 6: ['m', 'n', 'o'], 7: ['p', 'q', 'r', 's'], 8: ['t', 'u', 'v'], 9: ['w', 'x', 'y', 'z'] }; console.log(mapping); { '1': [], '2': [ 'a', 'b', 'c' ], '3': ...

Read More

Counting the number of 1s upto n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

Counting the number of 1s from 1 to n is a common algorithmic problem in JavaScript. We need to count how many times the digit "1" appears in all positive integers up to and including n. For example, if n = 31, the digit "1" appears in: 1, 10, 11 (twice), 12, 13, 14, 15, 16, 17, 18, 19, 21, 31. That's a total of 14 occurrences. Problem Analysis The challenge is to efficiently count digit occurrences without iterating through every number. We can solve this using digit-by-digit analysis or a simpler brute force approach. Method ...

Read More

Finding distance between two points in a 2-D plane using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 456 Views

Problem We are required to write a JavaScript function that takes in two objects both having x and y property specifying two points in a plane. Our function should find and return the distance between those two points using the Euclidean distance formula. Distance Formula The distance between two points (x₁, y₁) and (x₂, y₂) is calculated using: distance = √[(x₂ - x₁)² + (y₂ - y₁)²] Example Following is the code: const a = {x: 5, y: -4}; const b = {x: 8, y: 12}; const distanceBetweenPoints ...

Read More

Reshaping 2-D array in JavaScript

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

In JavaScript, reshaping a 2-D array means converting it to a new matrix with different dimensions while preserving the original element order. This is useful for data manipulation and matrix operations. Problem Statement We need to write a JavaScript function that takes a 2-D array and reshapes it into a new matrix with specified rows and columns. The elements should maintain their row-traversing order from the original array. For example, if we have: const arr = [ [6, 7], [8, 9] ]; const r = 1, c ...

Read More
Showing 3841–3850 of 8,392 articles
« Prev 1 383 384 385 386 387 840 Next »
Advertisements