AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 342 of 840

Converting whitespace string to url in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 720 Views

In web URLs, browsers automatically replace spaces with '%20' for proper encoding. JavaScript provides multiple methods to convert whitespace characters to URL-encoded format. We need to write a function that takes a string and replaces all whitespace characters with '%20'. For example, if the input string is: const str = 'some extra Space'; Then the output should be: const output = 'some%20extra%20%20Space'; Using Manual Loop Method This approach iterates through each character and manually replaces spaces: const str = 'some extra Space'; const replaceWhitespace = (str = ...

Read More

Calculating value of a sequence based on some input using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 189 Views

Problem We need to write a JavaScript function that takes a number n as input and calculates the value of a sequence term un. Given a sequence where: u1 = 0 and u2 = 2 Our function should find the value of un for which the following recurrence relation holds: 6unun+1 - 5unun+2 + un+1un+2 = 0 Understanding the Sequence From the recurrence relation, we can derive that this sequence follows the pattern un = 2n-1 for n ≥ 2, and u1 = 0. Example Implementation Here's the ...

Read More

Comparing array elements keeping count in mind in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

Suppose, we have two arrays of literals that contain the same number of elements. We are supposed to write a function that checks whether or not the both arrays contain the same elements appearing for the same number of times. If the arrays fulfil this condition, we return true, false otherwise. We will create a copy of the second array, and start iterating over the first array. As we iterate, we will keep deleting the elements from the second array that are present in first array. If during iteration we encounter any element that isn't present in second ...

Read More

Compressing string in JavaScript

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

We are required to write a JavaScript function that takes in a string that might contain some continuous repeating characters. The function should compress the string like this − 'wwwaabbbb' -> 'w3a2b4' 'kkkkj' -> 'k4j' And if the length of the compressed string is greater than or equal to the original string we should return the original string. For example − 'aab' can be compressed to 'a2b1' but it increases its length to 4 so our function should return 'aab' How It Works The algorithm iterates through each character, counting consecutive ...

Read More

Hours and minutes from number of seconds using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 479 Views

We are required to write a JavaScript function that takes in the number of seconds and returns the number of hours and minutes contained in those seconds. Problem Statement Given a number of seconds, we need to convert it into a readable format showing hours and minutes. Input: 3601 seconds Expected Output: "1 hour(s) and 0 minute(s)" Solution Here's how we can convert seconds to hours and minutes: const seconds = 3601; const toTime = (seconds = 60) => { const hR = 3600; // 1 hour ...

Read More

All ways to divide array of strings into parts in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 336 Views

In JavaScript, dividing an array into two non-empty parts can be accomplished through various approaches. This tutorial explores different methods to generate all possible ways to split an array of strings into exactly two parts. Problem Statement We need to create a JavaScript function that takes an array of strings with at least two elements and returns all possible ways to divide it into two non-empty parts. For example, with the array ["az", "toto", "picaro", "zone", "kiwi"], we want to generate: (az) | (toto picaro zone kiwi) (az toto) | (picaro zone kiwi) ...

Read More

Understanding the find() method to search for a specific record in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

The find() method searches through an array and returns the first element that matches a specified condition. It's perfect for locating specific records in arrays of objects. Syntax array.find(callback(element, index, array)) Parameters callback - Function to test each element element - Current element being processed index (optional) - Index of current element array (optional) - The array being searched Return Value Returns the first element that satisfies the condition, or undefined if no element is found. Example: Finding a Student Record var students = [ ...

Read More

Sum of two elements just less than n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a single number, num, as the second argument. The function should then find two such numbers whose sum is greatest in the array but just less than the number num. If there exists no two such numbers whose sum is less than num, our function should return -1. Problem Statement Given an array of numbers and a target value, find the maximum sum of any two elements that is still less than the target. For ...

Read More

Counting divisors of a number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 629 Views

We are required to write a JavaScript function that takes in a number and returns the count of its divisors. Problem Statement Input: const num = 30; Expected Output: const output = 8; Because the divisors of 30 are: 1, 2, 3, 5, 6, 10, 15, 30 Method 1: Simple Iteration Approach The straightforward approach is to iterate through all numbers from 1 to the given number and count those that divide evenly. const countDivisorsSimple = (num) => { ...

Read More

Sort nested array containing objects ascending and descending according to date in JavaScript

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

Suppose we have a JSON Object that contains a nested array like this: const arr = { "DATA": [ { "BookingID": "9513", "DutyStart": "2016-02-11 12:00:00" }, { "BookingID": "91157307", "DutyStart": "2016-02-11 13:00:00" }, { "BookingID": "95117317", "DutyStart": "2016-02-11 13:30:00" ...

Read More
Showing 3411–3420 of 8,392 articles
« Prev 1 340 341 342 343 344 840 Next »
Advertisements