- 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 9306 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
61 Views
Let’s say, we are given an array of numbers that contains first n natural numbers, but one element appears twice in the array, so the total number of elements is n+1. Our job is to write a function that takes in the array and returns the number that appears twice in linear time.Method 1: Using Array.prototype.reduce()This is a bit trickier approach but the most compressed in terms of code written. First, let’s see the code for it −const arr = [1, 4, 8, 5, 6, 7, 9, 2, 3, 7]; const duplicate = a => a.reduce((acc, val, ind) => val+acc- ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
306 Views
We are required to write a function that takes in an array of numbers and returns the difference between its highest and lowest number.At first, create an array −const arr = [23, 54, 65, 76, 87, 87, 431, -6, 22, 4, -454];Now, find maximum and minimum values with Math.max() and Math.min() methods, respectively −const arrayDifference = (arr) => { let min, max; arr.forEach((num, index) => { if(index === 0){ min = num; max = num; }else{ min = Math.min(num, min); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
693 Views
Let’s say, we are required to write a function that takes in two arrays and returns a new array that contains values in alternating order from first and second array. Here, we will just loop over both the arrays simultaneously picking values from them one after the other and feed them into the new array.The full code for doing the same will be −Exampleconst arr1 = [34, 21, 2, 56, 17]; const arr2 = [12, 86, 1, 54, 28]; let run = 0, first = 0, second = 0; const newArr = []; while(run < arr1.length + arr2.length){ if(first ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
697 Views
Let’s say, we have an array of strings in which each value each element has a dash (-), left to which we have our key and right to which we have our value. Our job is to split these strings and form an object out of this array.Here is the sample array −const arr = ["name-Rakesh", "age-23", "city-New Delhi", "jobType-remote", "language-English"];So, let’s write the code, it will loop over the array splitting each string and feeding it into the new objectThe full code will be −Exampleconst arr = ["name-Rakesh", "age-23", "city-New Delhi", "jobType-remote", "language-English"]; const obj = {}; arr.forEach(string => ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
247 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 = [23, 545, 43, 232, 32, 43, 23, 43];The output should be −const output = [545, 232, 32];Understanding the difference −Array.prototype.indexOf() → It returns the index of first occurrence of searched string if it exists, otherwise -1.Array.prototype.lastIndexOf() → It returns the index of last occurrence of searched string ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We are required to write a function that takes in a number between 1 and 26 (both inclusive) and returns the corresponding English alphabet for it. (capital case) If the number is out of this range return -1.For example −toAlpha(3) = C toAlpha(18) = RAnd so on.The ASCII CodesASCII codes are the standard numerical representation of all the characters and numbers present on our keyboard and many for.The capital English alphabets are also mapped in the ascii char codes, they start from 65 and goes all the way up to 90, with 65 being the value for ‘A’, 66 for ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
4K+ Views
Let’s say, we are given two objects that have similar key value pairs with one or key having different values in both objects. Our job is to write a function that takes in the two objects as argument and returns the very first key it finds having different values. If all the keys have exact same values, it should return -1.Here are the sample objects −const obj1 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, age: 45, salary: 190000, job: 'Full Stack Developer', employedSince: 2005 } const obj2 = { name: ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
244 Views
We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.So, the output should look like −const output = [1, 2, 7, 9, 5, 11, 21];Let’s write the function partialSum(). The full code for this function will be −Exampleconst arr = [1, 1, 5, 2, -4, 6, 10]; const partialSum = (arr) => { const output = []; arr.forEach((num, index) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Let’s say, we are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number.For example −findSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6So, the output should be 6.Let’s write the code for this function findSum() −Example// using recursion const findSum = (num) => { if(num < 10){ return num; } const lastDigit = num % 10; const remainingNum = Math.floor(num / 10); return findSum(lastDigit + findSum(remainingNum)); } console.log(findSum(2568));We check if the number is less than 10, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array.If the original array is −[ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ]Then the output should be −[60, 93, 30, 55]Let’s write a sample function addArray()The full code for this function will be −Exampleconst arr = [ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ]; const sumArray = (array) => { ... Read More