- 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 9312 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
102 Views
We are required to write a JavaScript function that takes in two arguments −An Array, say arr, of literals that may contain some repeating elements.A number, say limit.The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [4, 6, 7, 4, 2, 5, 7, 7, 4, 4, 3]; const validateElements = (arr, n) => { const counts = arr.reduce((acc, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
110 Views
We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
253 Views
Suppose, we have an array of literals like this −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];We are required to write a JavaScript function that takes in this array and a number, say n, and returns an object representing the count of elements greater than and smaller than n.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const smallerLargerNumbers = (arr, num) => { return arr.reduce((acc, val) => { let { greater, smaller ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −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));OutputThe output in the console will be −{ hi: 2, hello: 1 }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
130 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −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 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
125 Views
We are required to write a JavaScript function that should repeat the even number inside the same array.For example, given the following array −const arr = [1, 2, 5, 6, 8];OutputWe should get the output −const output = [1, 2, 2, 5, 6, 6, 8, 8];Therefore, let’s write the code for this function −ExampleThe code for this will be −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));OutputThe output in the console will be −[ 1, 2, 2, 5, 6, 6, 8, 8 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
106 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.Note: sequence a0, a1, ..., an is considered to be strictly increasing if a0 < a1 < ... < an. Sequences containing only one element are also considered to be strictly increasing.Example, for 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)
221 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)
60 Views
Suppose, we have an array and objects like these −Objects:const main = [ {name: "Karan", age: 34}, {name: "Aayush", age: 24}, {name: "Ameesh", age: 23}, {name: "Joy", age: 33}, {name: "Siddarth", age: 43}, {name: "Nakul", age: 31}, {name: "Anmol", age: 21}, ];Array:const names = ["Karan", "Joy", "Siddarth", "Ameesh"];We are required to write a JavaScript function that takes in two such arrays and filters the first array in place to contain only those objects whose name property is included in the second array.Therefore, let’s write the code for this function −ExampleThe code for this ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
312 Views
We are required to write a JavaScript function that takes in a string that represents a number.Replace the leading zero with spaces in the number. Make sure the prior spaces in number are retained.For example: If the string value is defined as −"004590808"Then the output should come as −"4590808"ExampleThe code for this will be −const str = ' 004590808'; const replaceWithSpace = str => { let replaced = ''; const regex = new RegExp(/^\s*0+/); replaced = str.replace(regex, el => { const { length } = el; return ' '.repeat(length); }); return replaced; }; console.log(replaceWithSpace(str));OutputThe output in the console will be −4590808