AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 465 of 840

Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 193 Views

We are required to write a JavaScript function that takes in three numbers A, B and N, and finds the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively. Understanding the Problem For an N-digit number, we need to: Calculate sum of digits at even positions (0-indexed) Calculate sum of digits at odd positions (0-indexed) Check if even position sum is divisible by A Check if odd position sum is divisible by B Helper Function: Calculate Position-wise Sums First, let's ...

Read More

Recursion problem Snail Trail in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

The snail trail problem involves traversing a 2D array in a spiral pattern from outside to inside. Given a square matrix, we need to create a flat array by following the spiral path: right → down → left → up, repeating until we reach the center. Suppose, we have an array like this: const arr = [ [1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7] ]; The array is bound to be a square matrix. We are required to ...

Read More

Passing empty parameter to a method in JavaScript

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

In JavaScript, you can pass empty parameters to a method by simply omitting them when calling the function. This is useful when working with functions that have default parameters or when you want to rely on the function's default behavior. Understanding Default Parameters Default parameters allow functions to have fallback values when no arguments are provided or when undefined is passed. Passing Empty Parameters body { ...

Read More

Find and return array positions of multiple values JavaScript

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

We have to write a function, say findPositions() that takes in two arrays as argument. And it should return an array of the indices of all the elements of the second array present in the first array. For example − If the first array is ['john', 'doe', 'chris', 'snow', 'john', 'chris'], And the second array is ['john', 'chris'] Then the output should be − [0, 2, 4, 5] Therefore, let's write the code for this function. We will use a forEach() loop here. Example const values = ['michael', ...

Read More

Compare two arrays and get those values that did not match JavaScript

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

We have two arrays of literals that contain some common values, our job is to write a function that returns an array with all those elements from both arrays that are not common. For example − // if the two arrays are: const first = ['cat', 'dog', 'mouse']; const second = ['zebra', 'tiger', 'dog', 'mouse']; // then the output should be: const output = ['cat', 'zebra', 'tiger'] // because these three are the only elements that are not common to both arrays Let's write the code for this − We will spread the two ...

Read More

Finding how many time a specific letter is appearing in the sentence using for loop, break, and continue - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentence using for loop, break, and continue statements. Using continue Statement The continue statement skips the current iteration and moves to the next one. Here's how we can use it to count character occurrences: const string = 'This is just an example string for the program'; const countAppearances = (str, char) => { let count = 0; for(let i = 0; i < str.length; i++){ ...

Read More

How to detect whether the browser is online or offline with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 491 Views

JavaScript provides the navigator.onLine property to detect whether the browser is online or offline. This property returns true when connected to the internet and false when offline. The navigator.onLine Property The navigator.onLine property is a read-only boolean that indicates the browser's online status: // Basic syntax if (navigator.onLine) { // Browser is online } else { // Browser is offline } Example: Manual Check Online/Offline Status Checker Check Status ...

Read More

What are Partial functions in JavaScript?

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

Partial functions in JavaScript allow you to create specialized versions of functions by pre-filling some of their arguments. They take a function and some arguments, returning a new function that remembers those arguments and waits for the remaining ones. This technique is useful for creating reusable function variants and implementing functional programming patterns like currying. Basic Partial Function Example Partial Functions Partial Functions in JavaScript ...

Read More

Construct string via recursion JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 389 Views

We are required to write a recursive function, say pickString that takes in a string that contains a combination of alphabets and numbers and returns a new string consisting of only alphabets. For example, If the string is 'dis122344as65t34er', The output will be: 'disaster' Therefore, let's write the code for this recursive function − Syntax const pickString = (str, len = 0, res = '') => { if(len < str.length){ const char = parseInt(str[len], 10) ? '' : str[len]; ...

Read More

Shift last given number of elements to front of array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 308 Views

In JavaScript, moving elements from the end of an array to the front is a common array manipulation task. This tutorial shows how to create a function that shifts the last n elements to the beginning of an array in-place. Problem Statement We need to create an array method reshuffle() that: Takes a number n (where n ≤ array length) Moves the last n elements to the front Modifies the array in-place Returns true on success, false if n exceeds array length Example Input and Output // Original array const arr = ["blue", ...

Read More
Showing 4641–4650 of 8,392 articles
« Prev 1 463 464 465 466 467 840 Next »
Advertisements