Object Oriented Programming Articles

Page 180 of 589

Adding a function for swapping cases to the prototype object of strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

In JavaScript, we can extend built-in data types by adding custom methods to their prototype objects. This allows us to create reusable functions that work with existing data types like strings, arrays, and objects. In this tutorial, we'll create a custom swapCase() method for strings that swaps the case of each character - converting uppercase letters to lowercase and vice versa, while keeping non-alphabetic characters unchanged. Understanding String Prototype Extension When we add a method to String.prototype, it becomes available to all string instances. The this keyword inside the method refers to the string that called the ...

Read More

Sorting or Arranging an Array with standard array values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

We are required to sort a dynamic JavaScript array. The condition is that we are required to sort it according to the values stored in a particular order in a standard predefined array. Let's say the following is our dynamic array − const dbArray = ['Apple', 'Banana', 'Mango', 'Apple', 'Mango', 'Mango', 'Apple']; And suppose the standard array against which we have to sort the above array is like − const stdArray = ['Mango', 'Apple', 'Banana', 'Grapes']; So, after sorting the dbArray, my resultant array should look like − const ...

Read More

If ([] == false) is true, why does ([] || true) result in []? - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 600 Views

If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following − In the first case, we are using loose conditional checking, allowing type coercion to take over. While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hood. Let's now unveil the conversions that happen behind the scenes in both cases. Case 1 - ([] == false) According to the MDN docs, when two data ...

Read More

How to access variables declared in a function, from another function using JavaScript?

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

In JavaScript, variables declared inside a function are scoped to that function and cannot be directly accessed from outside. However, there are several ways to make these variables available to other functions or globally. Problem with Function Scope Variables declared inside a function are private to that function: function myFunction() { let localVar = "I'm inside the function"; } myFunction(); // console.log(localVar); // This would cause an error Using Constructor Functions with 'this' You can use constructor functions to expose internal variables as properties: const num ...

Read More

Finding the intersection of arrays of strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 814 Views

We need to find the intersection of two arrays of strings and return an array containing the common elements. Each element in the result should appear as many times as it shows in both arrays. For example − If input is − arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate']; Then the output should be − ['world', 'you'] Approach For unsorted arrays, we need to check every value of the first array against the second array. This approach has O(n²) time complexity. We ...

Read More

Dividing an array – JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 683 Views

Let's say, we are required to write a function that takes in an array arr of string / number literals as the first argument and a number n as second argument. We are required to return an array of n subarrays, each of which contains at most arr.length / n elements. And the distribution of elements should be like this − The first element goes in the first subarray, second in second, third in third and so on. Once we have one element in each subarray, we again start ...

Read More

Summing all the unique values of an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 346 Views

Let's say, we are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index. For example − If the input array is − const input = [1, 3, 1, 3, 5, 7, 5, 4]; Then the output should be − const output = [2, 6, 10, 7, 4]; That means all the duplicate ones are summed to index 0 (1 + 1 = 2), all the duplicate threes are summed to index 1 (3 + ...

Read More

Finding the difference between two arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 614 Views

Finding the difference between two arrays means identifying elements that exist in one array but not in the other. This is a common operation when comparing datasets or finding unique values. We have two arrays of numbers like these: const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; We need to write a JavaScript function that takes in two such arrays and returns the elements that are not common to both arrays. Using indexOf() Method The traditional approach uses nested loops ...

Read More

JavaScript - Writing a string function to replace the kth appearance of a character

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 290 Views

Let's say, we are required to write a String.prototype function that takes in three arguments. First argument is string that should be searched for substrings Second argument is the string, the occurrence of which String to be removed Third argument is a Number say n, nth occurrence of substring to be removed from string. The function should return the new string if the removal of the subStr from the string was successful, otherwise it should return -1 in all cases. Example Following is the code − const str = 'jkdsttjkdsre'; const subStr ...

Read More

Removing redundant elements from array altogether - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 377 Views

We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array. For example, if the input is: const arr = [763, 55, 43, 22, 32, 43, 763, 43]; The output should be: const output = [55, 22, 32]; Understanding the Approach We will be using the following two methods: ...

Read More
Showing 1791–1800 of 5,881 articles
« Prev 1 178 179 180 181 182 589 Next »
Advertisements