- 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)
1K+ Views
We are required to write a JavaScript function that takes in a number which may be an integer or a floating-point number. If it's a floating-point number, we have to return the count of numbers after the decimal point. Otherwise we should return 0.For our example, we are considering two numbers −const num1 = 1.123456789; const num2 = 123456789;ExampleFollowing is the code −const num1 = 1.123456789; const num2 = 123456789; const decimalCount = num => { // Convert to String const numStr = String(num); // String Contains Decimal if (numStr.includes('.')) { return numStr.split('.')[1].length; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
391 Views
We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array.Let’s say the following is our array −const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42];ExampleFollowing is the code −const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; const dividesAll = el => { const result = []; let num; for (num = Math.floor(el / 2); num > 1; num--){ if (el % num === 0) { result.push(num); } }; return result; }; const dividesArray = arr => { return arr.map(dividesAll).reduce((acc, val) => { return acc.filter(el => val.includes(el)); }); }; console.log(dividesArray(arr));OutputThis will produce the following output in console −[ 2 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array with using the Array.prototype.sort() method. We are required to use the Array.prototype.reduce() method to sort the array.Let’s say the following is our array −const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98];ExampleFollowing is the code −// we will sort this array but // without using the array sort function // without using any kind of conventional loops // using the ES6 function reduce() const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
282 Views
We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenatedFor example: If the number is −9119Then the output should be −811181because 9^2 is 81 and 1^2 is 1.ExampleFollowing is the code −const num = 9119; const squared = num => { const numStr = String(num); let res = ''; for(let i = 0; i < numStr.length; i++){ const square = Math.pow(+numStr[i], 2); res += square; }; return res; }; console.log(squared(num));OutputThis will produce the following output in console −811181
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
151 Views
We are required to write a JavaScript function takes in an array of numbers. The function should bring all the 3-digit integers to the front of the array.Let’s say the following is our array of numbers −const numList = [1, 324,34, 3434, 304, 2929, 23, 444];ExampleFollowing is the code −const numList = [1, 324,34, 3434, 304, 2929, 23, 444]; const isThreeDigit = num => num > 99 && num < 1000; const bringToFront = arr => { for(let i = 0; i < arr.length; i++){ if(!isThreeDigit(arr[i])){ continue; }; arr.unshift(arr.splice(i, 1)[0]); }; }; bringToFront(numList); console.log(numList);OutputThis will produce the following output in console −[ 444, 304, 324, 1, 34, 3434, 2929, 23 ]
![Nikitasha Shrivastava](https://www.tutorialspoint.com/assets/profiles/655298/profile/60_3988199-1684213136.jpeg)
805 Views
In the given problem statement we are presented with a number and we have to sum up the given number until it becomes one digit and implement the code in Javascript for getting the required sum. Understanding the Problem In the given program we will have a number and our task is to repeatedly sum its digits until the outcome of the number becomes a single digit. For example suppose we have a number like 874563 and we need to add all of its digits (8 + 7 + 4 + 5 + 6 + 3 = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array.Let’s say the following are our arrays −const arr1 = [2, 6, 7, 1, 7, 8, 4, 3]; const arr2 = [5, ,7, 2, 2, 1, 3]; const arr3 = [1, 56, 345, 6, 54, 2, 68, 85, 3];ExampleFollowing is the code −const arr1 = [2, 6, 7, 1, 7, 8, 4, 3]; const arr2 = [5, ,7, 2, 2, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Suppose, we have an array of objects like this −const people = [ {"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"}, {"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"}, {"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"}, {"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"} ];We are required to write a JavaScript function that takes in one such array and sorts the array in place, according to the age property of each object in increasing order.Therefore, the output should look something like this −const output = [ {"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"}, {"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"}, {"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"}, {"id":1, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
229 Views
We are required to write a JavaScript function that takes in an array, a start index and an end index. The function should reverse the portion of the array between the start index and end index.For example −If the array is −const arr = [2, 6, 5, 8, 3, 5, 2, 6, 7];And the start index and end index are 3, 7 respectively, then the array should be reversed to −const output = [2, 6, 5, 2, 5, 3, 8, 6, 7];ExampleFollowing is the code −const arr = [2, 6, 5, 8, 3, 5, 2, 6, 7]; const start = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
174 Views
In Mathematics, the symmetric difference of two sets, say A and B is represented by A △ BAnd it is defined as the set of all those elements which belongs either to A or to B but not to both.For example −const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, 9];Then the symmetric difference of A and B will be −const diff = [2, 4, 9]ExampleFollowing is the code −const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, ... Read More