Object Oriented Programming Articles

Page 4 of 589

JavaScript Get English count number

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

Finding smallest number using recursion in JavaScript

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

Subarrays product sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each. The function should calculate the product of each subarray and then add both the results together. Example If the input array is: const arr = [1, 2, 3, 4, 5, 6] The calculation would be: (1*2*3) + (4*5*6) 6 + 120 126 Using Array.reduce() Method Here's ...

Read More

JavaScript Strings: Replacing i with 1 and o with 0

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 278 Views

We are required to write a function that takes in a string as one and only argument and returns another string that has all 'i' and 'o' replaced with '1' and '0' respectively. It's one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through. Using For Loop The most straightforward approach is to iterate through each character and build a new string: const string = 'Hello, is it raining in Amsterdam?'; const replaceChars = (str) => { ...

Read More

Add line break inside a string conditionally in JavaScript

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

In JavaScript, you can add line breaks inside a string conditionally by checking character limits and replacing spaces with newline characters. This is useful for formatting text within specific width constraints. Problem Description We need to create a function breakString() that takes two parameters: a string to be broken and a threshold number representing the maximum characters per line. When the character count exceeds this limit and we encounter a space, we replace it with a line break. Algorithm Approach The solution iterates through the string while maintaining a character count. When the count reaches the ...

Read More

Reverse alphabetically sorted strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 364 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 606 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 269 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 337 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 315 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
Showing 31–40 of 5,881 articles
« Prev 1 2 3 4 5 6 589 Next »
Advertisements