AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 387 of 840

Converting km per hour to cm per second using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 761 Views

We are required to write a JavaScript function that takes in a number that specifies speed in kmph and it should return the equivalent speed in cm/s. Understanding the Conversion To convert km/h to cm/s, we need to understand the relationship: 1 kilometer = 100, 000 centimeters 1 hour = 3, 600 seconds Formula: km/h × (100, 000 cm / 1 km) × (1 hour / 3, 600 seconds) Example Following is the code: const kmph = 12; const convertSpeed = (kmph) => { const secsInHour = ...

Read More

Finding count of special characters in a string in JavaScript

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

In JavaScript, you can count special characters in a string by checking each character against a predefined set of special characters. This is useful for text validation, content analysis, or data processing tasks. Problem Statement We need to count occurrences of these special characters in a string: '!', ', ', ''', ';', '"', '.', '-', '?' The function should iterate through each character and increment a counter when it finds a match. Example Implementation const str = "This, is a-sentence;.Is this a sentence?"; const countSpecial = str => { ...

Read More

Sort array according to the date property of the objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

In JavaScript, sorting an array of objects by date requires converting date strings to Date objects for proper comparison. The Array.sort() method with a custom comparator function handles this efficiently. Sample Data Consider an array of objects with date properties: const arr = [ {date: "2016-06-08 18:10:00"}, {date: "2016-04-26 20:01:00"}, {date: "2017-02-06 14:38:00"}, {date: "2017-01-18 17:30:21"}, {date: "2017-01-18 17:24:00"} ]; Using Array.sort() with Date Comparison The most straightforward approach uses Array.sort() with a ...

Read More

Check if the string is a combination of strings in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 268 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and a string as the second argument. The function should check whether the string specified by second argument can be formed by combining the strings of the array in any possible way. For example − If the input array is − const arr = ["for", "car", "keys", "forth"]; And the string is − const str = "forthcarkeys"; Then the output should be true, because the string is a combination of elements ...

Read More

Finding the group with largest elements with same digit sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 194 Views

We are required to write a JavaScript function that takes in a positive integer, say n, as the only argument. The function should first group the integers from 1 to n into subarrays where each subarray contains all elements with the same digit sum. Then the function should examine each subarray and return the length of the largest group (the group with the most elements). Understanding Digit Sum The digit sum is calculated by adding all digits of a number. For example: Digit sum of 15 = 1 + 5 = 6 Digit sum of 23 ...

Read More

Inverting signs of integers in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

Problem We are required to write a JavaScript function that takes in an array of integers (negatives and positives). Our function should convert all positives to negatives and all negatives to positives and return the resulting array. Method 1: Using for Loop The traditional approach uses a for loop to iterate through each element and multiply by -1 to invert the sign: const arr = [5, 67, -4, 3, -45, -23, 67, 0]; const invertSigns = (arr = []) => { const res = []; for(let i = ...

Read More

Enter values with prompt and evaluate on the basis of conditions in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 299 Views

JavaScript's prompt() function allows you to collect user input and evaluate it using conditional statements. This is useful for creating interactive web applications that respond based on user-provided values. Basic Syntax To get user input and convert it to a number: var value = parseInt(prompt("Enter a value")); Example: Conditional Evaluation Based on User Input Here's a complete example that prompts for two values and evaluates their product: Prompt and Evaluate ...

Read More

Sorting an array of literals using quick sort in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 459 Views

We are required to write a JavaScript function that takes in an array of numbers and uses the quick sort algorithm to sort it. QuickSort Algorithm QuickSort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the array and partitioning other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. How QuickSort Works The algorithm follows these steps: Choose a pivot element from the array (typically the middle element) Partition the ...

Read More

Dynamic Programming: Is second string subsequence of first JavaScript

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

We are given two strings str1 and str2, we are required to write a function that checks if str1 is a subsequence of str2. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. For example, "ace" is a subsequence of "abcde" while "aec" is not because the relative order is not maintained. Understanding Subsequences In a subsequence, characters must appear in the same order as in the original string, but they ...

Read More

How to build a string with no repeating character n separate list of characters? in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 473 Views

When working with multiple arrays of characters, we often need to create combinations where each string contains exactly one character from each array with no duplicate characters. This is useful for generating unique combinations while avoiding repeated letters that might exist across different arrays. Problem Overview Given multiple arrays of characters, we need to build all possible strings that: Contains exactly one letter from each array ...

Read More
Showing 3861–3870 of 8,392 articles
« Prev 1 385 386 387 388 389 840 Next »
Advertisements