- 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 9318 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
160 Views
We are required to write a JavaScript function that counts all unique items in an array. The function should return an object representing the count of each unique element of the array.Let’s say the following is our array −const arr = ["hi", "hello", "hi"];ExampleFollowing is the code −const arr = ["hi", "hello", "hi"]; const countUnique = arr => { const counts = {}; for (var i = 0; i < arr.length; i++) { counts[arr[i]] = 1 + (counts[arr[i]] || 0); }; return counts; }; console.log(countUnique(arr));OutputThis will produce the following output on console −{ hi: 2, hello: 1 }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Suppose, we have an array like this −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];We are required to write a JavaScript function that takes in one such array and a number, say n.The function should return the index of item from the array which is closest to the number n.ExampleFollowing is the code −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; const closestIndex = (num, arr) => { let curr = arr[0], diff = Math.abs(num - curr); let index = 0; for (let val = 0; val ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
63 Views
We are required to write a JavaScript function that should repeat the even number inside the same array.Therefore, for example given the following array −const arr = [1, 2, 5, 6, 8];We should get the output −const output = [1, 2, 2, 5, 6, 6, 8, 8];ExampleFollowing is the code −const arr = [1, 2, 5, 6, 8]; const repeatEvenNumbers = arr => { let end = arr.length -1; for(let i = end; i > 0; i--){ if(arr[i] % 2 === 0){ arr.splice(i, 0, arr[i]); }; }; return arr; }; console.log(repeatEvenNumbers(arr));OutputThis will produce the following output on console −[ 1, 2, 2, 5, 6, 6, 8, 8 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
268 Views
A number is gapful if it is at least 3 digits long and is divisible by the number formed by stringing the first and last numbers together. The smallest number that fits this description is 100. First digit is 1, last digit is 0, forming 10, which is a factor of 100. Therefore, 100 is gapful.We are required to create a function that takes a number n and returns the closest gapful number (including itself). If there are 2 gapful numbers that are equidistant to n, return the lower one.Some examples −gapful(25) ➞ 100 gapful(100) ➞ 100 gapful(103) ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
329 Views
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.The sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.ExampleFor sequence = [1, 3, 2, 1], the output should be −almostIncreasingSequence(sequence) = false.There is no one element in this array that can be removed in order to get a strictly increasing sequence.For sequence = [1, 3, 2], the output ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
108 Views
Suppose, we have an array like this −const arr = [ [1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7] ];The array is bound to be a square matrix.We are required to write a JavaScript function that takes in this array and constructs a new array by taking elements and spiraling in until it converges to center. A snail trail spiraling around the outside of the matrix and inwards.Therefore, the output for the above array should be −const output = [1, 2, 3, 4, 5, 6, 7, 8, 9, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
73 Views
We are required to write a JavaScript function that takes in a string and turns it into a Mexican Wave i.e. resembling string produced by successive captial letters in every word −For example −If the string is −const str = 'edabit';Then the output should be the following i.e. successive single capital letter −const output = ["Edabit", "eDabit", "edAbit", "edaBit", "edabIt", "edabiT"];ExampleFollowing is the code −const str = 'edabit'; const replaceAt = function(index, char){ let a = this.split(""); a[index] = char; return a.join(""); }; String.prototype.replaceAt = replaceAt; const createEdibet = word => { let array = word.split('') ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
315 Views
Suppose, we have an array of objects like this −const homes = [ { "h_id": "3", "city": "Dallas", "state": "TX", "zip": "75201", "price": "162500" }, { "h_id": "4", "city": "Bevery Hills", "state": "CA", "zip": "90210", "price": "319250" }, { "h_id": "5", "city": "New York", "state": "NY", "zip": "00010", "price": "962500" } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
148 Views
Suppose we have an object like this −const obj = { name: "Ramesh", age: 34, occupation: "HR Manager", address: "Tilak Nagar, New Delhi", experience: 13 };We are required to write a JavaScript function on Objects that computes their size (i.e., the number of properties in it).ExampleFollowing is the code −const obj = { name: "Ramesh", age: 34, occupation: "HR Manager", address: "Tilak Nagar, New Delhi", experience: 13 }; Object.prototype.size = function(obj) { let size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)){ size++ }; }; return size; }; const size = Object.size(obj); console.log(size);This will produce the following output on console −5
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
227 Views
Let’s say, we have an object as follows −const myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" };We are required to illustrate the best way to remove the property regex to end up with new myObject?Following is the solution −const myObject = { "ircEvent": "PRIVMSG", "method": "newURI" };The delete operator is used to remove properties from objects.const myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; delete myObject['regex']; console.log(myObject.hasOwnProperty("regex")); // falseThe delete operator in JavaScript has a different function to that of the keyword in C and C++ −It ... Read More