- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 9309 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
542 Views
We are here required to write a JavaScript function that takes in an array of numbers and returns another array with the factorial of corresponding elements of the array. We will first write a recursive method that takes in a number and returns its factorial and then we will iterate over the array, calculating the factorial of each element of array and then finally we will return the new array of factorials.Therefore, let’s write the code for thisExampleconst arr = [4, 8, 2, 7, 6, 20, 11, 17, 12, 9]; const factorial = (num, fact = 1) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
384 Views
Let’s say, we are required to write a function that takes in an array and a number and returns the index of the first element of the first pair from the array that adds up to the provided number, if there exists no such pair in the array, we have to return -1.By pair, we mean, two consecutive elements of the array and not any two arbitrary elements of the array. So, let’s write the code for this function −Exampleconst arr = [4, 8, 2, 7, 6, 42, 41, 77, 32, 9]; const findPair = (arr, num) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
330 Views
Usually when we convert a decimal to hexadecimal (base 16) we use the set 0123456789ABCDEF to map the number.We are required to write a function that does exactly the same but provides user the freedom to use any scale rather than the one mentioned above.For example −The hexadecimal notation of the decimal 363 is 16B But if the user decides to use, say, a scale ‘qwertyuiopasdfgh’ instead of ‘0123456789ABCDEF’, the number 363, then will be represented by wusThat’s what we are required to do.So, let’s do this by making a function toHex() that makes use of recursion to build a ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
424 Views
Consider, we have the following array of objects −const arr = [{ id: 121, hobby: 'cycling' }, { id: 125, hobby: 'jogging' }, { id: 129, hobby: 'reading' }, { id: 121, hobby: 'writing' }, { id: 121, hobby: 'playing football' }, { id: 125, hobby: 'cooking' }, { id: 129, hobby: 'horse riding' }];Let’s say, we have to write a function that takes in such an array and merges it based on the common id property, and for the hobby property we assign it an ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
638 Views
Happy NumberA happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. Whereas if during this process any number gets repeated, the cycle will run infinitely and such numbers are called unhappy numbers.For example − 13 is a happy number because, 1^2 + 3^2 = 10 and, 1^2 + 0^2 = 1On the other hand, 36 is an unhappy number.We are required to write a function that uses recursion to determine whether or not a number is a happy number.So, let’s write this function out. The key to this function ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
284 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
257 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
281 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
298 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
121 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