AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 339 of 840

How to form a two-dimensional array with given width (columns) and height (rows) in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 458 Views

We are required to write a JavaScript function that takes in three arguments: height --> no. of rows of the array width --> no. of columns of the array val --> initial value of each element of the array Then the function should return the new array formed based on these criteria. Method 1: Using Array.apply() with map() This approach creates arrays using Array.apply() and fills them with map(): const rows = 4, cols = 5, val = 'Example'; const fillArray = (width, height, value) => { ...

Read More

Finding average of n top marks of each student in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 655 Views

Suppose, we have an array of objects that contains information about some students and the marks scored by them over a period of time like this: const marks = [ { id: 231, score: 34 }, { id: 233, score: 37 }, { id: 231, score: 31 }, { id: 233, score: 39 }, { id: 231, score: 44 }, { id: 233, score: 41 }, { id: 231, score: ...

Read More

Finding longest consecutive joins in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

We are required to write a JavaScript function that takes in an array of pairs of numbers, arr, as the first and the only argument. In every pair, the first number is always smaller than the second number. Now, we define a pair (c, d) that can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion. Our function is supposed to find the length longest chain which can be formed. Problem Example For example, if the input to the function is: const arr ...

Read More

Calculating the LCM of multiple numbers in JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers of any length and returns their LCM (Least Common Multiple). We will approach this problem in parts: Part 1 − We will create a helper function to calculate the Greatest Common Divisor (GCD) of two numbers Part 2 − Then using Part 1 helper function we will create another helper function to calculate the Least Common Multiple (LCM) of two numbers. Part 3 − Finally, using Part 2 helper function we will create a function that loops over the array and ...

Read More

Calculating average of a sliding window in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 694 Views

We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a number, num (num < length of arr) as the second argument. The function should construct and return a new array that contains the average of all possible num contiguous numbers of the array. For example − If the input array and the number are − const arr = [1, 2, 3, 4, 5]; const num = 2; Then the output should be − const output = [1.5, 2.5, 3.5, 4.5]; ...

Read More

Replacing dots with dashes in a string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 602 Views

We are required to write a JavaScript function that takes in a string and replaces all appearances of dots(.) in it with dashes(-). Input const str = 'this.is.an.example.string'; Expected Output const output = 'this-is-an-example-string'; All appearances of dots(.) in string str are replaced with dash(-) Method 1: Using replace() with Regular Expression The most efficient approach is using the built-in replace() method with a global regular expression: const str = 'this.is.an.example.string'; const replaceDots = (str = '') => { return str.replace(/\./g, '-'); }; ...

Read More

Returning the first number that equals its index in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 174 Views

Problem We need to write a JavaScript function that takes an array of numbers and returns the first number whose value equals its 0-based index position. This means we're looking for an element where array[i] === i. Example Following is the code − const arr = [9, 2, 1, 3, 6, 5]; const findFirstSimilar = (arr = []) => { for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(el === i){ ...

Read More

Maximum average of a specific length of subarray in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

Problem We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a number, num, as the second argument. Our function should find the contiguous subarray of given length num that has the maximum average value. And we need to output the maximum average value. Example Input and Output For example, if the input to the function is: const arr = [1, 12, -5, -6, 50, 3]; const num = 4; The expected output is: 12.75 Output Explanation: ...

Read More

How to calculate days left until next Christmas using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 928 Views

In this article, you will learn how to calculate the days remaining until the next Christmas using JavaScript. We'll use the Date object to work with dates and perform time calculations to determine how many days are left until December 25th. Understanding the Logic To calculate days until Christmas, we need to: Get today's date Determine the next Christmas date (this year or next year) Calculate the time difference in milliseconds Convert milliseconds to days Method 1: Direct Calculation In this approach, we calculate the time difference without using functions: let todayDate ...

Read More

How to sort an array of integers correctly in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 454 Views

To sort an array of integers correctly in JavaScript, use the sort() method with a comparison function. Without a comparison function, sort() converts numbers to strings, causing incorrect ordering. The Problem with Default sort() JavaScript's default sort() method converts elements to strings and sorts alphabetically, which doesn't work correctly for numbers: var numbers = [10, 2, 100, 5]; console.log("Default sort:", numbers.sort()); Default sort: [ 10, 100, 2, 5 ] Correct Integer Sorting Use a comparison function that returns the difference between two numbers: var arrayOfIntegers = [67, 45, ...

Read More
Showing 3381–3390 of 8,392 articles
« Prev 1 337 338 339 340 341 840 Next »
Advertisements