Object Oriented Programming Articles

Page 6 of 589

Finding shortest word in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 894 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

Retaining array elements greater than cumulative sum using reduce() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

We need to write a JavaScript function that takes an array of numbers and returns a new array containing only elements that are greater than the cumulative sum of all previous elements. We'll solve this using the Array.prototype.reduce() method. Problem Understanding For each element in the array, we compare it with the sum of all elements that came before it. If the current element is greater than this cumulative sum, we include it in the result array. Example Let's implement the solution using reduce(): const arr = [1, 2, 30, 4, 5, 6]; ...

Read More

Removing duplicate values in a twodimensional array in JavaScript

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

When working with two-dimensional arrays in JavaScript, you may need to remove duplicate values that appear across different sub-arrays. This article shows how to create a function that removes all duplicate values from a 2D array, keeping only the first occurrence of each value. Problem Statement We need to write a JavaScript function that takes a two-dimensional array and returns a new array where duplicate values across all sub-arrays are removed, preserving only the first occurrence of each value. Example Here's how to implement this functionality: const arr = [ ...

Read More

String replace multiple characters with an asterisk in JavaScript

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

We are required to write a JavaScript function that takes in a string as the first argument and an array of numbers. Our function should replace all the characters in the string at indices that are specified by the array elements taken as the second argument with an asterisk. Example The code for this will be − const str = "Lorem ipsum dolor sit amet consectetur adipiscing elit"; const arr = [4, 7, 9, 12, 15]; const replaceWithAsterisk = (str, indices) => { let res = ''; res ...

Read More

Grouping and sorting 2-D array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 928 Views

Suppose we have a two-dimensional array of numbers like this − const arr = [ [1, 3, 2], [5, 2, 1, 4], [2, 1] ]; We are required to write a JavaScript function that groups all the identical numbers into their own separate subarray, and then the function should sort the group array to place the subarrays into increasing order. Therefore, finally the new array should look like − const output = [ [1, 1, 1], [2, 2, ...

Read More

How to sort array according to age in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 476 Views

We are required to write a JavaScript function that takes in an array of numbers representing ages of some people. Then the function should bring all the ages less than 18 to the front of the array without using any extra memory. Understanding the Problem We need to sort an array so that all minors (ages < 18) appear before adults (ages ≥ 18), while maintaining the original order within each group. Example The code for this will be − const ages = [23, 56, 56, 3, 67, 8, 4, 34, 23, 12, ...

Read More

Padding a string with random lowercase alphabets to fill length in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 313 Views

We are required to write a function that takes in two arguments, first is a string and second is a number. The length of string is always less than or equal to the number. We have to insert some random lowercase alphabets at the end of the string so that its length becomes exactly equal to the number and we have to return the new string. Example Let's write the code for this function — const padString = (str, len) => { if(str.length < len){ ...

Read More

JavaScript Array: Checking for multiple values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 295 Views

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not. Example The code for this will be — const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const multipleIncludes = (first, second) => { const indexArray = first.map(el => { return second.indexOf(el); }); return indexArray.indexOf(-1) === -1; } console.log(multipleIncludes(arr1, ...

Read More

Consecutive elements sum array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 577 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array. For example, if the input array is − const arr1 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9]; Then the output should be − const output = [2, 9, 9, 13, 17] How It Works The function pairs consecutive elements: (1+1=2), (2+7=9), (4+5=9), (6+7=13), (8+9=17). If the array has an odd length, the last ...

Read More
Showing 51–60 of 5,881 articles
« Prev 1 4 5 6 7 8 589 Next »
Advertisements