- 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)
2K+ Views
We are given an array of string / number literals. We are required to create a function removeRandom() that takes in the array and recursively removes one random item from the array and simultaneously printing it until the array contains items.This can be done through creating a random number using Math.random() and removing the item at that index using Array.prototype.splice() and printing it until the length of array shrinks to 0.Here is the code for doing the same −Exampleconst arr = ['Arsenal', 'Manchester United', 'Chelsea', 'Liverpool', 'Leicester City', 'Manchester City', 'Everton', 'Fulham', 'Cardiff City']; const removeRandom = (array) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
383 Views
We are given a string and are required to write a function that returns the frequency of each character in the array. And we should not take the case of characters into consideration.To do this the best way would be iterating over the string and preparing an object with key as characters and their frequency as value.The code for doing this will be −Exampleconst string = 'ASASSSASAsaasaBBBASvcdNNSASASxxzccxcv'; const countFrequency = str => { const frequency = {}; for(char of str.toLowerCase()){ if(!frequency[char]){ frequency[char] = 1; }else{ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
435 Views
We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array.So, let’s write the code for this. We will iterate over the array, checking for the existence of each element in the object, if we found a string that’s not a key of object, we exit and return false, otherwise we return true.Here is the code for doing the same −Exampleconst obj = { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
877 Views
We are required to write a function reverseObject() that takes in an object and returns an object where keys are mapped to values.We will approach this by iterating over Object.keys() and pushing key value pair as value key pair in the new object.Here is the code for doing so −Exampleconst cities = { 'Jodhpur': 'Rajasthan', 'Alwar': 'Rajasthan', 'Mumbai': 'Maharasthra', 'Ahemdabad': 'Gujrat', 'Pune': 'Maharasthra' }; const reverseObject = (obj) => { const newObj = {}; Object.keys(obj).forEach(key => { if(newObj[obj[key]]){ newObj[obj[key]].push(key); }else{ newObj[obj[key]] = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
220 Views
We are required to write a function that takes in two numbers and returns their product, but without using the (*) operator.Trick 1: Using Divide Operator TwiceWe know that multiplication and division are just the inverse of each other, so if we divide a number by other number’s inverse, won’t it be same as multiplying the two numbers?Let’s see the code for this −const a = 20, b = 45; const product = (a, b) => a / (1 / b); console.log(product(a, b));Trick 2: Using LogarithmsLet’s examine the properties of logarithms first −log(a) + log(b) = log(ab)So, let’s use this ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
202 Views
We are given an object that contains some random properties, including some numbers, boolean, strings and the object itself.We are required to write a function that takes in the object as first argument and a string as second argument, possible value for second argument is a name of any data type in JavaScript like number, string, object, boolean, symbol etc.Our task is to delete every property of type specified by the second argument. If the second argument is not provided, take ‘number’ as default.The full code for doing so will be −const obj = { name: 'Lokesh Rahul', ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
621 Views
We have an array of numbers that contains some undefined and null values as well. We are required to make a function, say quickSum that takes in the array and returns its quick sum, ignoring the undefined and null values.The full code for doing so will be −Exampleconst arr = [23,566,null,90,-32,undefined,32,-69,88,null]; const quickSum = (arr) => { const sum = arr.reduce((acc, val) => { return acc + (val || 0); }, 0); return sum; }; console.log(quickSum(arr));OutputThe output in the console will be −698
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
635 Views
Let’s say, we have an array of arrays, each containing some numbers along with some undefined and null values. We are required to create a new array that contains the sum of each corresponding sub array elements as its element. And the values undefined and null should be computed as 0.Following is the sample array −const arr = [[ 12, 56, undefined, 5 ], [ undefined, 87, 2, null ], [ 3, 6, 32, 1 ], [ undefined, null ]];The full code for this problem will be −Exampleconst arr = [[ 12, 56, undefined, 5 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
290 Views
Let’s say, we have to write a function −replaceChar(str, arr, [char])Now, replace all characters of string str that are not present in array of strings arr with the optional argument char. If char is not provided, replace them with ‘*’.Let’s write the code for this function.The full code will be −Exampleconst arr = ['a', 'e', 'i', 'o', 'u']; const text = 'I looked for Mary and Samantha at the bus station.'; const replaceChar = (str, arr, char = '*') => { const replacedString = str.split("").map(word => { return arr.includes(word) ? word : char; }).join(""); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
Let’s say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday.Following is our array −const weather = [{ day: 'Wednesday', humidity: 60 }, { day: 'Saturday', humidity: 50 }, { day: 'Thursday', humidity: 65 }, { day: 'Monday', humidity: 40 }, { day: 'Sunday', humidity: ... Read More