- 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 9321 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
155 Views
We are required to write a JavaScript function that takes in an array of Integers. The function should return the length of the longest decreasing subsequence from the array.For example −If the input array is −const arr = [5, 2, 5, 4, 3, 2, 4, 6, 7];Then the output should be −const output = 4;because the longest decreasing subsequence (of consecutive words) is [5, 4, 3, 2];Exampleconst arr = [5, 2, 5, 4, 3, 2, 4, 6, 7]; const decreasingSequence = (arr = []) => { let longest = []; let curr = []; const setDefault = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
277 Views
We are required to write a JavaScript function that in any number of arguments (all of Number type).The function should calculate all possible sums of addition and subtraction.For example − If the arguments are 1, 2, 3Then all possible combinations are −1 + 2 + 3 1 - 2 - 3 1 + 2 - 3 1 - 2 + 3Finally, the function should the sum that is closest to 0. In this case, that answer would just be 0.Exampleconst findSmallestPositive = (...arr) => { let set = new Set([Math.abs(arr[0])]); for (let i = 1; i < ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
226 Views
We are required to write a JavaScript function that takes in a read only array of n + 1 integers between 1 and n.The function should find one number that repeats in linear time and using at most O(n) space.For example If the input array is −const arr = [3 4 1 4 1];Then the output should be −const output = 1;If there are multiple possible answers ( like above ), we should output any one. If there is no duplicate, we should output -1.Exampleconst arr = [3, 4, 1, 4, 1]; const findRepeatedNumber = (arr = []) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
712 Views
We are required to write a JavaScript function that takes in an array of Numbers. The function should apply the algorithm of quicksort to sort the array either in increasing or decreasing order.QuickSort AlgorithmQuicksort follows the below steps −Step 1 − Make any element as the pivot (preferably first or last, but any element can be the pivot)Step 2 − Partition the array on the basis of pivotStep 3 − Apply a quick sort on the left partition recursivelyStep 4 − Apply a quick sort on the right partition recursivelyThe average and best case time complexity of QuickSort are O(nlogn) ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
135 Views
Suppose, we have an array that contains some demo credit card numbers like this −const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];We have been tasked with creating a function that takes in this array. The function must return the credit card number with the greatest sum of digits.If two credit card numbers have the same sum, then the last credit card number should be returned by the function.ExampleThe code for this will be −const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']; const findGreatestNumber = (arr) => { let n, i = 0, sums; sums = []; while (i < ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Suppose, we have the following array of objects −const arr = [ {"2015":11259750.05}, {"2016":14129456.9} ];We are required to write a JavaScript function that takes in one such array. The function should prepare an array of arrays based on the input array.Therefore, the output for the above array should look like −const output = [ [2015,11259750.05], [2016,14129456.9] ];ExampleThe code for this will be −const arr = [ {"2015":11259750.05}, {"2016":14129456.9} ]; const mapToArray = (arr = []) => { const res = []; arr.forEach(function(obj,index){ const key= Object.keys(obj)[0]; const value = parseInt(key, 10); res.push([value, obj[key]]); }); return res; }; console.log(mapToArray(arr));OutputAnd the output in the console will be −[ [ 2015, 11259750.05 ], [ 2016, 14129456.9 ] ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
491 Views
We are required to write a JavaScript program that provides users an input to fill in a number.And upon filling when the user clicks the button, we should display the sum of all the digits of the number.ExampleThe code for this will be −JavaScript Code −function myFunc() { var num = document.getElementById('digits').value; var tot = 0; num.split('').forEach( function (x) { tot = tot + parseInt(x,10); }); document.getElementById('output').innerHTML = tot; }HTML Code − Submit OutputAnd the output will be &miuns;After clicking “Submit” −
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
237 Views
We have a mixed array that we need to sort by alphabet and then by digit −const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105'];ExampleThe code for this will be −const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105']; const alphaNumericSort = (arr = []) => { arr.sort((a, b) => { const aPart = a.split('-'); const bPart = b.split('-'); return aPart[0].toLowerCase().localeCompare(bPart[0].toLowerCase()) || aPart[1] - bPart[1]; }); }; alphaNumericSort(arr); console.log(arr);OutputAnd the output in the console will be −[ 'Ab-1', 'ab-2', 'ab-3', 'ab-10', ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
157 Views
Suppose, we have a 2-D array like this −const arr = [ [3, 1], [2, 12], [3, 3] ];We are required to write a JavaScript function that takes in one such array.The function should then create a new 2-D array that contains all elements initialized to undefined other than the element's index present in the input array.Therefore, for the input array, output[3][1] = 1; output[2][12] = 1; output[3][3] = 1;And rest all elements should be initialized to undefinedTherefore, the final output should look like −const output = [ undefined, undefined, [ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
Suppose we have the following array of objects −const arr = [ { 'name' : 'd', 'index' : 3 }, { 'name' : 'c', 'index' : 2 }, { 'name' : 'a', 'index' : 0 }, { 'name' : 'b', 'index' : 1 } ];We are required to write a JavaScript function that takes in one such array.The function should sort this array in increasing order according to ... Read More