Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 4 of 589
JavaScript Get English count number
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 MoreFinding smallest number using recursion in JavaScript
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 MoreSubarrays product sum in JavaScript
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 MoreJavaScript Strings: Replacing i with 1 and o with 0
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 MoreAdd line break inside a string conditionally in JavaScript
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 MoreReverse alphabetically sorted strings in JavaScript
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 MoreFinding the first unique element in a sorted array in JavaScript
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 MoreSum of distinct elements of an array in JavaScript
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 MoreReturn the index of first character that appears twice in a string in JavaScript
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 MoreFind array elements that are out of order in JavaScript
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