- 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)
3K+ Views
We have a complex json file that we have to handle with JavaScript to make it hierarchical, in order to later build a tree.Every entry of the JSON array has −id − a unique id, parentId − the id of the parent node (which is 0 if the node is a root of the tree)level − the level of depth in the treeThe JSON data is already "ordered", means that an entry will have above itself a parent node or brother node, and under itself a child node or a brother node.The input array is −const arr = [ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
645 Views
Suppose, we have the following array of arrays −const arr = [ [ ['dog', 'Harry'], ['age', 2] ], [ ['dog', 'Roger'], ['age', 5] ] ];We are required to write a JavaScript function that takes in one such nested array. The function should then prepare an object based on the array.The object for the above array should look like −const output = [ {dog: 'Harry', age: 2}, {dog: 'Roger', age: 5} ];ExampleThe code for this will be −const arr = [ [ ['dog', 'Harry'], ['age', ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
334 Views
We are required to write a JavaScript function that takes in an array of literal values. The function should then count the frequency of each element of the input array and prepare a new array on that basis.For example − If the input array is −const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];Then the output should be −const output = [ [5, 3], [2, 5], [9, 1], [4, 1] ];ExampleThe code for this will be −const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; const frequencyArray = (arr = []) => { const res = []; arr.forEach(el => { if (!this[el]) { this[el] = [el, 0]; res.push(this[el]) }; this[el][1] ++ }, {}); return res; }; console.log(frequencyArray(arr));OutputAnd the output in the console will be −[ [ 5, 3 ], [ 2, 5 ], [ 9, 1 ], [ 4, 1 ] ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
770 Views
Suppose, we have a reference to an object −let test = {};This object will potentially (but not immediately) have nested objects, something like −test = {level1: {level2: {level3: "level3"}}};We are required to write a JavaScript function that takes in one such object as the first argument and then any number of key strings as the arguments after.The function should determine whether or not the nested combination depicted by key strings exist in the object.ExampleThe code for this will be −const checkNested = function(obj = {}){ const args = Array.prototype.slice.call(arguments, 1); for (let i = 0; i < args.length; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
546 Views
We are required to write a JavaScript function that takes in a string. The function should count the number of vowels present in the string.The function should prepare an object mapping the count of each vowel against them.ExampleThe code for this will be −const str = 'this is an example string'; const vowelCount = (str = '') => { const splitString=str.split(''); const obj={}; const vowels="aeiou"; splitString.forEach((letter)=>{ if(vowels.indexOf(letter.toLowerCase()) !== -1){ if(letter in obj){ obj[letter]++; }else{ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
343 Views
We are required to write a JavaScript function that takes two arrays, say arr1 and arr2. Our function should return a sorted array in lexicographical order of the strings of arr1 which are substrings of strings of arr2.ExampleThe code for this will be −const lexicographicalSort = (arr1 = [], arr2 = []) => { let i, j; const res = []; outer: for (j = 0; j < arr1.length; j++) { for (i = 0; i < arr2.length; i++) { if (arr2[i].includes(arr1[j])) { res.push(arr1[j]); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
267 Views
We are required to write a JavaScript function that takes in an array of strings. The function should find all the substring and superstring combinations that exist in the array and return an array of those elements.For example − If the array is −const arr = ["abc", "abcd", "abcde", "xyz"];Then the output should be −const output = ["abc", "abcd", "abcde"];because the first two are the substring of last.ExampleThe code for this will be −const arr = ["abc", "abcd", "abcde", "xyz"]; const findStringCombinations = (arr = []) => { let i, j, res = {}; for (i = 0; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
98 Views
We are required to write a JavaScript function that takes in an array of literal values. Our function should then return the highest occurrence of an array value, and if there's an equal occurrence, we should return the first selected value of the equal occurrences.const arr = ['25', '50', 'a', 'a', 'b', 'c']In this case, we should return 'a'const arr = ['75', '100', 'a', 'b', 'b', 'a']In this case, I should also get 'a'ExampleThe code for this will be −const arr = ['25', '50', 'a', 'a', 'b', 'c']; const arr1 = ['75', '100', 'a', 'b', 'b', 'a']; const getMostFrequentValue = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
421 Views
We are required to write a JavaScript function that takes in a string. Our function is supposed to count the number of alphabets (uppercase or lowercase) in the array.For example − If the input string is −const str = 'this is a string!';Then the output should be −13ExampleThe code for this will be −const str = 'this is a string!'; const isAlpha = char => { const legend = 'abcdefghijklmnopqrstuvwxyz'; return legend.includes(char); }; const countAlphabets = (str = '') => { let count = 0; for(let i = 0; i < str.length; i++){ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
948 Views
Suppose, we have an array of objects like this −const arr = [ { id: 1, score: 1, isCut: false, dnf: false }, { id: 2, score: 2, isCut: false, dnf: false }, { id: 3, score: 3, isCut: false, dnf: false }, { id: 4, score: 4, isCut: false, dnf: false }, { id: 5, score: 5, isCut: true, dnf: true }, { id: 6, score: 6, isCut: true, dnf: false }, { id: 7, score: 7, isCut: true, dnf: false }, { id: 8, score: 8, isCut: true, dnf: false ... Read More