Javascript Articles

Page 65 of 534

Reverse alphabetically sorted strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 362 Views

We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse alphabetical order i.e., 'z' should come before 'y', 'b' before 'a', and so on. Example If the input string is: const str = "hello"; Then the output should be: const output = "ollhe"; Using Custom Comparator Function Here's how we can achieve reverse alphabetical sorting using a custom comparator: const string = 'hello'; const sorter = (a, b) => { const legend = ...

Read More

Finding the first unique element in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 603 Views

Suppose we have a sorted array of literals like this: const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false. For this array, the output should be 6. Using Index Comparison Method Since the array is sorted, we can check if an element is unique by comparing it with ...

Read More

Sum of distinct elements of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 266 Views

Suppose, we have an array of numbers like this − const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array. For example: The output for the array mentioned above will be − 30 Method 1: Using lastIndexOf() and indexOf() This approach checks if the current index is the last occurrence of each element, ensuring we count each distinct element ...

Read More

Return the index of first character that appears twice in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

We are required to write a JavaScript function that takes in a string and returns the index of the first character that appears twice in the string. If there is no such character then we should return -1. Syntax function firstRepeating(str) { // Implementation here } Example The code for this will be − const str = 'Hello world, how are you'; const firstRepeating = str => { const map = new Map(); for(let i = 0; i < ...

Read More

Find array elements that are out of order in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 312 Views

Suppose we have an array of sorted numbers but some elements of the array are out of their sorted order. We are required to write a JavaScript function that takes in one such array and returns a subarray of all those elements that are out of order. Example The code for this will be − const arr = ["2", "3", "7", "4", "5", "6", "1"]; const findOutOfOrder = arr => { let notInOrder = []; notInOrder = arr.filter((el, ind) => { ...

Read More

Grouping names based on first letter in JavaScript

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

Suppose, we have an array of names like this: const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", "James"]; We are required to write a JavaScript function that takes in one such array. The function should return an array of objects with two properties: letter -> the letter on which the names are grouped names -> an array of names that falls in that group Example The code for this will be: const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", ...

Read More

How to iterate over objects in array and sum a property in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 902 Views

Suppose we have an array of objects like this: const arr = [ { duration: 10, any: 'fields' }, { duration: 20, any: 'other fields' }, { duration: 15, any: 'some other fields' } ...

Read More

Get values that are not present in another array in JavaScript

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

We are given two arrays: (arr1 and arr2) − arr1 contains some literal values. arr2 contains objects that map some literal values. We are required to write a JavaScript function that takes in two such arrays. Then the function should return an array of all the elements from arr1 that are not mapped by objects in arr2. Example The code for this will be − const arr1 = [111, 222, 333, 444]; const arr2 = [ { identifier: 111 }, ...

Read More

Finding shortest word in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 865 Views

We are required to write a JavaScript function that takes in a string and returns the shortest word from the string. For example: If the input string is: const str = 'This is a sample string'; Then the output should be: 'a' Using Array.reduce() Method This approach splits the string into words and uses reduce() to find the shortest word by comparing lengths: const str = 'This is a sample string'; const findSmallest = str => { const strArr = str.split(' '); ...

Read More

Map numbers to characters in JavaScript

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

When mapping numbers to characters in JavaScript, we need to convert digits to their corresponding alphabetical positions (1='a', 2='b', etc.). A number can be mapped in multiple ways by grouping digits differently. For example, the number 12145 can be interpreted as: 1, 2, 1, 4, 5 → a, b, a, d, e 12, 1, 4, 5 → l, a, d, e 12, 14, 5 → l, n, e However, combinations like 1, 2, 1, 45 are invalid because 45 exceeds the alphabet range (1-26). Solution Approach We use recursion to explore all valid digit ...

Read More
Showing 641–650 of 5,340 articles
« Prev 1 63 64 65 66 67 534 Next »
Advertisements