- 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 9316 Articles for Object Oriented Programming
![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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
4K+ Views
We are required to write a JavaScript function that takes in a number and calculates its square root without using the Math.sqrt() function.Therefore, let’s write the code for this function −ExampleThe code for this will be −const square = (n, i, j) => { let mid = (i + j) / 2; let mul = mid * mid; if ((mul === n) || (Math.abs(mul - n) < 0.00001)){ return mid; }else if (mul < n){ return square(n, mid, j); }else{ return square(n, i, mid); } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
636 Views
We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string.For example:a = 1 b = 2 c = 3 d = 4 e =5 . . . y = 25 z = 25Note: Remove any special characters and spaces.So, if the input is −"hello man"Then the output should be −"8, 5, 12, 12, 15, 13, 1, 14"ExampleThe code for this will be −const str = 'hello man'; const charPosition = str => { str = str.split(''); const arr = []; const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
100 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −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));OutputThe output in the console will be −[ 2 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
7K+ Views
We are required to write a JavaScript function that takes in an array of numbers.The function should sort the array using the Array.prototype.sort() method, but, here, we are required to use the Array.prototype.reduce() method to sort the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const sortWithReduce = arr => { return arr.reduce((acc, val) => { let ind = 0; while(ind < arr.length && val < arr[ind]){ ind++; } acc.splice(ind, 0, val); return acc; }, []); }; console.log(sortWithReduce(arr));OutputThe output in the console will be −[ 98, 57, 89, 37, 34, 5, 56, 4, 3 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
178 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 concatenated.For example: If the number is −99Then the output should be −8181because 9^2 is 81 and 1^2 is 1.Therefore, let’s write the code for this function −ExampleThe code for this will be −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); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
284 Views
We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number.We are required to do so without converting the number to String or any other data type.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 546767643; const sumDigit = (num, sum = 0) => { if(num){ return sumDigit(Math.floor(num / 10), sum + (num % 10)); } return sum; }; const sumRepeatedly = num => { while(num > 9){ num = sumDigit(num); }; return num; }; console.log(sumRepeatedly(num));OutputThe output in the console will be −3
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
452 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −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]; const intersection = (arr1, arr2) => { const res = []; for(let ... Read More