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 339 of 840
How to form a two-dimensional array with given width (columns) and height (rows) in JavaScript ?
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 MoreFinding average of n top marks of each student in JavaScript
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 MoreFinding longest consecutive joins in JavaScript
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 MoreCalculating the LCM of multiple numbers in JavaScript
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 MoreCalculating average of a sliding window in JavaScript
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 MoreReplacing dots with dashes in a string using JavaScript
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 MoreReturning the first number that equals its index in an array using JavaScript
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 MoreMaximum average of a specific length of subarray in JavaScript
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 MoreHow to calculate days left until next Christmas using JavaScript?
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 MoreHow to sort an array of integers correctly in JavaScript?
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