AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 369 of 840

JavaScript Get English count number

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

We are required to write a JavaScript function that takes in a number and returns an English ordinal number for it (1st, 2nd, 3rd, 4th, etc.). Example 3 returns 3rd 21 returns 21st 102 returns 102nd How Ordinal Numbers Work English ordinal numbers follow these rules: Numbers ending in 1: add "st" (1st, 21st, 31st) — except 11th Numbers ending in 2: add "nd" (2nd, 22nd, 32nd) — except 12th Numbers ending in 3: add "rd" (3rd, 23rd, 33rd) — except 13th All others: add "th" (4th, 5th, 6th, 7th, 8th, 9th, ...

Read More

AND product of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 193 Views

We have an array of arrays of boolean values like this: const arr = [[true, false, false], [false, false, false], [false, false, true]]; We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator. Let's write the code for this function. We will be using Array.prototype.reduce() function to achieve this. Example The code for this will be: const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge ...

Read More

Sorting Array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 483 Views

Sorting an array based on another array's order is a common JavaScript task. This technique allows you to reorder elements according to a reference array's sequence. Problem Statement Given two arrays, we need to sort the first array based on the order of elements in the second array: const input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"]; console.log("Original array:", input); console.log("Reference order:", sortingArray); Original array: [ 'S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8' ] Reference order: [ 'S-1', ...

Read More

JavaScript: create an array of JSON objects from linking two arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 541 Views

Suppose, we have two arrays like these − const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ]; We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array. Therefore, the output for the above arrays should look like − { "breakfast" : ["eggs", "yogurt", "toast"], ...

Read More

Contiguous subarray with 0 and 1 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 357 Views

In JavaScript, finding the longest contiguous subarray with equal numbers of 0s and 1s in a binary array is a classic problem that can be solved efficiently using a hash map approach. Problem Statement Given a binary array containing only 0s and 1s, find the length of the longest contiguous subarray that contains an equal number of 0s and 1s. For example, with the input array: const arr = [1, 0, 0, 1, 0, 1, 0, 0]; console.log("Input array:", arr); Input array: [1, 0, 0, 1, 0, 1, 0, 0] ...

Read More

Sending personalised messages to user using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 Views

We need to write a JavaScript function that sends personalized messages based on whether the user is the owner or a regular user. The function takes two parameters: the user name and the owner name. Problem Statement Create a function that compares the user name with the owner name and returns appropriate greetings: If the user is the owner: return "Hello master" If the user is different from owner: return "Hello" followed by the user's name Solution Here's the implementation using a simple conditional statement: const name = 'arnav'; const owner ...

Read More

Finding smallest number using recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns the smallest number from it using recursion. Let's say the following are our arrays: const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; Recursive Approach The recursive solution uses a helper function that compares the first element with the rest of the array: const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; const min = arr => { ...

Read More

Listing all the prime numbers upto a specific number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 252 Views

We are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers up to n. For example: If the number n is 24. Then the output should be − const output = [2, 3, 5, 7, 11, 13, 17, 19, 23]; Therefore, let's write the code for this function − Method 1: Using Helper Function for Prime Check This approach uses a helper function to check if a number is prime, then iterates through numbers up to n: ...

Read More

Forming the longest word in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 343 Views

We are required to write a JavaScript function that takes in a random English alphabet string, str, as the first argument and an array of strings, arr, as the second argument. The task of our function is to try deleting some characters from the string str and check which longest word can be formed that exists in the array arr as well. Our function should return the longest possible string. If there exists no such string, we should return an empty string. Problem Example For example, if the input to the function is: const ...

Read More

Numbers and operands to words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

We are required to write a JavaScript function that takes in a string of some mathematical operation and return its literal wording. Problem Converting mathematical expressions like "5 - 8" into readable words like "Five Minus Eight" requires mapping numbers and operators to their text equivalents. Approach We'll create two lookup objects: one for operators and another for numbers. Then parse the input string and convert each part using these mappings. Example Following is the code − const str = '5 - 8'; const convertToWords = (str = '') => { ...

Read More
Showing 3681–3690 of 8,392 articles
« Prev 1 367 368 369 370 371 840 Next »
Advertisements