AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 323 of 840

Average with the Reduce Method in JavaScript

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

In JavaScript, calculating the average of array elements using the reduce() method is an efficient and elegant approach. The reduce() method iterates through the array once to compute the sum, which we then divide by the array length. Understanding the Problem We need to calculate the average value of all elements in an array using JavaScript's reduce() method. For example, given the array [1, 2, 3, 4, 5], the average would be (1 + 2 + 3 + 4 + 5) / 5 = 15 / 5 = 3. Algorithm Step 1: Create a function that ...

Read More

Trim off '?' from strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

We are required to write a JavaScript function that takes in a string as the only argument. The string is likely to contain question marks (?) in the beginning and the end. The function should trim off all these question marks from the beginning and the end keeping everything else in place. For example − If the input string is − const str = '??this is a ? string?'; Then the output should be − 'this is a ? string' Method 1: Using Regular Expressions The simplest approach uses ...

Read More

Finding nth element of the Padovan sequence using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 303 Views

Padovan Sequence The Padovan sequence is a sequence of integers P(n) defined by the initial values: P(0) = P(1) = P(2) = 1 and the recurrence relation: P(n) = P(n-2) + P(n-3) The first few values of P(n) are: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, ... Problem We need to write a JavaScript function that takes in a number n and returns the nth term of the Padovan sequence. Using Iterative Approach The most efficient approach uses iteration to calculate the sequence step by step: const padovan = (num = 1) => { // Handle base cases if (num

Read More

How to calculate the date three months prior using JavaScript?

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

To calculate the date three months prior using JavaScript, we will first need to create a new date object, which will represent the current date. We will then use the setMonth() method to subtract 3 from the current month. Finally, we will convert this new date object back to a string using the toString method to display the date three months prior. Basically, we will be writing a dynamic JavaScript function that can take in a number as its input and return the date prior to that number of months from today's date. For example − ...

Read More

Grouping array values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

Suppose we have an array of objects containing some years and weeks data like this: const arr = [ {year: 2017, week: 45}, {year: 2017, week: 46}, {year: 2017, week: 47}, {year: 2017, week: 48}, {year: 2017, week: 50}, {year: 2017, week: 52}, {year: 2018, week: 1}, {year: 2018, week: 2}, {year: 2018, week: 5} ]; We are required to ...

Read More

Compare two arrays of single characters and return the difference? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 543 Views

We are required to compare, and get the difference, between two arrays containing single character strings appearing multiple times in each array. Example of two such arrays are: const arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T']; We will check each character at the same position and return only the parts who are different. Approach The algorithm compares elements at the same index position. When characters differ, both are added to the result array. Any remaining elements from the longer array are also included. Example ...

Read More

Insert a character at nth position in string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 659 Views

We are required to write a JavaScript function that takes in a string as the first argument and a number as the second argument and a single character as the third argument, let's call this argument char. The number is guaranteed to be smaller than the length of the array. The function should insert the character char after every n characters in the string and return the newly formed string. For example − If the arguments are − const str = 'NewDelhi'; const n = 3; const char = ' '; Then the ...

Read More

Mean of an array rounded down to nearest integer in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

Problem We need to write a JavaScript function that takes an array of numbers and returns the average (mean) of the array rounded down to the nearest integer using Math.floor(). Example Following is the code − const arr = [45, 23, 67, 68, 12, 56, 99]; const roundedMean = (arr = []) => { const { sum, count } = arr.reduce((acc, val) => { let { sum, count } = acc; count++; ...

Read More

Comparing the performance of recursive and looped factorial function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

We will be writing two JavaScript functions, the job of both the functions will be to take in a number and return its factorial. The first function should make use of a for loop or while loop to compute the factorial. Whereas the second function should compute the factorial using a recursive approach. Lastly, we should compare the times taken by these functions over a large number of iterations. Iterative Factorial Function The iterative approach uses a simple for loop to calculate the factorial: const factorial = (num = 1) => { ...

Read More

How to calculate the XOR of array elements using JavaScript?

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

We will use a for loop to iterate through the array. We will initialize a variable called "result" with the value of the first element in the array. For each subsequent element in the array, we will use the XOR operator to update the value of "result" with that element. This process will continue until all elements in the array have been processed, resulting in the final XOR value of all elements in the array. Let us first understand what XOR is. We will also see how XOR operation on an array works. Understanding Array XOR ...

Read More
Showing 3221–3230 of 8,392 articles
« Prev 1 321 322 323 324 325 840 Next »
Advertisements