
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

391 Views
Consider the following array of numbers −const arr = [10, 5, 6, 12, 7, 1];The sum of its consecutive elements taking one less element in every go will be −[10, 5, 6, 12, 7, 1] = 10 + 5 + 6 + 12 + 7 + 1 = 41; [5, 6, 12, 7, 1] = 5 + 6 + 12 + 7 + 1 = 31; [6, 12, 7, 1] = 6 + 12 + 7 + 1 = 26; [12, 7, 1] = 12 + 7 + 1 = 20; [7, 1] = 7 + 1 = 8; [1] ... Read More

363 Views
Let’s say, we are required to write an array method that overwrites the default Array.prototype.slice(). Usually the Array.prototype.slice() method takes in two arguments the start index and the end index, and returns a subarray of the original array from index start to end-1.What we wish to do is make this slice() function like so it returns a subarray from index start to end and not end-1. Therefore, the code for doing this is shown below. We iterate over the array using a for loop which is in fact is faster than any of the array methods we have. Then return ... Read More

361 Views
Let’s say, we are required to write a function that takes in an array of string / number literals and returns the index of the item that appears for the most number of times. We will iterate over the array and prepare a frequencyMap and from that map we will return the index that makes most appearances.The code for doing so will be −Exampleconst arr1 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34, 65, 34, 22, 67, 34]; const arr2 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34]; const mostAppearances = (arr) => { ... Read More

357 Views
We have to write a function that returns true if a portion of string1 can be rearranged to string2. Write function, say scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.For example −Let’s say string1 is str1 and string2 is str2. str1 is 'cashwool' and str2 is ‘school’ the output should return true. str1 is 'katas' and str2 is 'steak' should return false.So, here is the code for doing this. We simply split and sort the two strings and then check whether the smaller string is a substring of ... Read More

175 Views
Let’s say, we have an array that contains some data about the speed of a motor boat during upstreams and downstreams like this −Following is our sample array −const arr = [{ direction: 'upstream', velocity: 45 }, { direction: 'downstream', velocity: 15 }, { direction: 'downstream', velocity: 50 }, { direction: 'upstream', velocity: 35 }, { direction: 'downstream', velocity: 25 }, { direction: 'upstream', velocity: 40 }, { direction: 'upstream', velocity: 37.5 }]We are required to write a function that takes in such type of array ... Read More

179 Views
Let’s say, we are given a string and an array. Our job is to split the string according to the corresponding elements of the array. For example −Inputconst string = 'Javascript splitting string by given array element'; const arr = [2, 4, 5, 1, 3, 1, 2, 3, 7, 2];Output['Ja', 'vasc', 'ript ', 's', 'pli', 't', 'ti', 'ng ', 'string ', 'by']Let’s write a function, say splitAtPosition that takes in the string and the array and makes use of the Array.Prototype.reduce() method to return the splitted array.The code for this function will be −Exampleconst string = 'Javascript splitting string by ... Read More

145 Views
We are required to write a function that returns a random hex color. So here is the code for doing so −Exampleconst generateRandomColor = () => { const keys = '0123456789ABCDEF'; let color = ''; while(color.length < 6){ const random = Math.floor(Math.random() * 16); color += keys[random]; }; return `#${color}`; }; console.log(generateRandomColor()); console.log(generateRandomColor()); console.log(generateRandomColor()); console.log(generateRandomColor());OutputThe output in the console will be −#C83343 #D9AAF3 #9D55CC #28AE22

157 Views
We are required to write a function, say sumBetween() that takes in an array [a,b] of two elements and returns the sum of all the elements between a and b including a and b.For example −[4, 7] = 4+5+6+7 = 22 [10, 6] = 10+9+8+7+6 = 40Let’s write the code for this function −Exampleconst arr = [10, 60]; const sumUpto = (n) => (n*(n+1))/2; const sumBetween = (array) => { if(array.length !== 2){ return -1; } const [a, b] = array; return sumUpto(Math.max(a, b)) - sumUpto(Math.min(a, b)) + Math.min(a,b); }; console.log(sumBetween(arr)); console.log(sumBetween([4, 9]));OutputThe output in the console will be −1785 39

447 Views
We are required to write a recursive function, say pushRecursively(), which takes in an array of numbers and returns an object containing odd and even properties where odd is an array of odd numbers from input array and even an array of even numbers from input array. This has to be done using recursion and no type of loop method should be used.Exampleconst arr = [12, 4365, 76, 43, 76, 98, 5, 31, 4]; const pushRecursively = (arr, len = 0, odd = [], even = []) => { if(len < arr.length){ arr[len] % 2 === ... Read More

675 Views
We have an array of string / number literals that may/may not contain repeating characters. Our job is to write a function that takes in the array and returns the index of the first repeating character. If the array contains no repeating characters, we should return -1.So, let’s write the code for this function. We will iterate over the array using a for loop and use a map to store distinct characters as key and their index as value, if during iteration we encounter a repeating key we return its index otherwise at the end of the loop we return ... Read More