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 10 of 589
Inserting empty string in place of repeating values in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. For example: If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. Therefore, let's write the code for this function − Example The code for this will be − const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => { ...
Read MoreCumulative average of pair of elements in JavaScript
We have an array of numbers and need to write a function that returns an array with the average of each element and its predecessor. For the first element, since there's no predecessor, we return the element itself. Let's implement this using the Array.prototype.map() method to transform each element based on its position. How It Works The algorithm works as follows: For the first element (index 0): return the element itself For other elements: calculate (current + previous) / 2 Example const arr = [3, 5, 7, 8, 3, 5, 7, ...
Read MoreExpanding Numerals in JavaScript
We are required to write a function that, given a number, say, 123, will output an array − [100, 20, 3] Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function. Understanding Place Values Each digit in a number has a place value based on its position. For example, in 123: 1 is in the hundreds place (1 × 100 = 100) 2 is in the ...
Read MoreFiltering array to contain palindrome elements in JavaScript
We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. For example If the input array is − const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be − const output = [12321, 'did']; We will create a helper function that takes in a number or a string and checks if it's a palindrome or not. Then we will loop over the array, filter ...
Read MoreSorting a JSON object in JavaScript
In JavaScript, objects themselves cannot be sorted since their properties don't have a guaranteed order. However, we can extract the values from a JSON object and sort them into an array. Suppose we have an object like this: const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; We need to write a JavaScript function that takes this object and returns a sorted array of its values: const arr = [11, 23, 56, ...
Read MoreConstructing an object from repetitive numeral string in JavaScript
Suppose, we have a string with digits like this − const str = '11222233344444445666'; We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string. So, for this string, the output should be − const output = { "1": 2, "2": 4, "3": 3, "4": 7, "5": 1, "6": 3 }; Using a for Loop The most straightforward approach is to iterate ...
Read MoreFinding upper elements in array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument. Therefore, let's write the code for this function — Using for Loop The most straightforward approach uses a for loop to iterate through the array and check each element: const arr = [56, 34, 2, 7, 76, 4, 45, 3, ...
Read MoreInverting slashes in a string in JavaScript
When working with strings in JavaScript, you may need to replace forward slashes (/) with backslashes (\) or vice versa. This is common when dealing with file paths or URL manipulation. This article demonstrates how to create a function that takes a string containing forward slashes and converts them to backslashes. Problem Statement We need to write a JavaScript function that: Takes a string that may contain forward slashes (/) Returns a new string with all forward slashes replaced by backslashes (\) Method 1: Using a for Loop Here's a solution using a ...
Read MorePicking out uniques from an array in JavaScript
Suppose we have an array that contains duplicate elements like this − const arr = [1, 1, 2, 2, 3, 4, 4, 5]; We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array. Therefore, let's write the code for this function − Method 1: Using indexOf() and lastIndexOf() This approach compares the first and last occurrence of each element. If they're different, the element appears multiple times: ...
Read MoreMerging two arrays in a unique way in JavaScript
We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays. For example If the two arrays are − const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; Then the output should be − [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3] Using Index-Based Approach The code for this will be − const arr1 = [4, 3, 2, ...
Read More