- 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)
1K+ Views
We are required to write a JavaScript function that takes in a number and returns an array of all the prime numbers that exactly divide the input number.For example, if the input number is 18.Then the output should be −const output = [2, 3];ExampleLet’s write the code for this function −const num = 18; const isPrime = (n) => { for(let i = 2; i { const res = num % 2 === 0 ? [2] : []; let start = 3; while(start
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
160 Views
We are required to write a JavaScript function that takes in a string and returns a new string with all the character of the original string just the whitespaces removed.ExampleLet’s write the code for this function −const str = "This is an example string from which all whitespaces will be removed"; const removeWhitespaces = str => { let newStr = ''; for(let i = 0; i < str.length; i++){ if(str[i] !== " "){ newStr += str[i]; }else{ newStr += ''; }; }; return newStr; }; console.log(removeWhitespaces(str));OutputThe output in the console after removing whitespaces −Thisisanexamplestringfromwhichallwhitespaceswillberemoved
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
478 Views
We are required to write a JavaScript function that takes in an array of numbers and uses the quick sort algorithm to sort it.QuickSortThis algorithm is basically a divide and conquer algorithm where we pick a pivot in every pass of loop and put all the elements smaller than pivot to its left and all greater than pivot to its right (if its ascending sort otherwise opposite)ExampleLet’s write the code for this function −const arr = [43, 3, 34, 34, 23, 232, 3434, 4, 23, 2, 54, 6, 54]; // Find a "pivot" element in the array to compare all ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
485 Views
In the English language, all these characters are considered as punctuations −'!', "," ,"\'" ,";" ,"\"", ".", "-" ,"?"We are required to write a JavaScript function that takes in a string and count the number of appearances of these punctuations in the string and return that count.ExampleLet’s write the code for this function −const str = "This, is a-sentence;.Is this a sentence?"; const countPunctuation = str => { const punct = "!,\;\.-?"; let count = 0; for(let i = 0; i < str.length; i++){ if(!punct.includes(str[i])){ continue; }; count++; }; return count; }; console.log(countPunctuation(str));OutputThe output in the console: −5
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
260 Views
We are required to write a JavaScript function that takes in two 2-D arrays and returns a boolean based on the check whether the arrays are equal or not.The equality of these arrays, in our case, is determined by the equality of corresponding elementsBoth the arrays should have same number of rows and columns −arr1[i][j] === arr2[i][j]The above should yield true for all i between [0, number of rows] and j between [0, number of columns]ExampleLet’s write the code for this function −const arr1 = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], ]; const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
5K+ Views
TransposeThe transpose of a matrix (2-D array) is simply a flipped version of the original matrix (2-D array). We can transpose a matrix (2-D array) by switching its rows with its columns.Let’s say the following is our 2d array −const arr = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], ];Let’s write the code for this function −ExampleFollowing is the code −const arr = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], ]; const transpose = arr => { for (let i = 0; i < arr.length; i++) { for (let j = 0; j < i; j++) { const tmp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = tmp; }; } } transpose(arr); console.log(arr);OutputThe output in the console: −[ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
569 Views
We have an array of literals that contains some duplicate values appearing for many times like this −const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4];We are required to write a JavaScript function that takes in this array and pick out all the duplicate entries from the original array and only once.So, for the above array, the output should be −const output = [1, 4, 3, 2];ExampleLet’s write the code for this function −const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; const pickDuplicate = arr => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
288 Views
Disarium Number − All those numbers which satisfy the following equation are dDisarium number −xy...z = x^1 + y^2 + ... + z^nWhere n is the number of digits in the number.For example −175 is a disarium number be: 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175Let’s write the code for this function −ExampleFollowing is the code −const num = 175; const isDisarium = num => { const res = String(num) .split("") .reduce((acc, val, ind) => { acc += Math.pow(+val, ind+1); return acc; }, 0); return res === num; }; console.log(isDisarium(num)); console.log(isDisarium(32)); console.log(isDisarium(4334));OutputThe output in the console: −true false false
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
We are required to write a JavaScript function that takes in a number (representing the number of days) and returns an object with three properties, namely −weeks, months, years, daysAnd the properties should have proper values of these four properties that can be made from the number of days. We should not consider leap years here and consider all years to have 365 days.For example −If the input is 738, then the output should be −const output = { years: 2, months: 0, weeks: 1, days: 1 }ExampleLet’s write the code for this function −const days ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
A number is called Armstrong number if the following equation holds true for that number −xy..z = x^n + y^n+.....+ z^nWhere, n denotes the number of digits in the numberFor example − 370 is an Armstrong number because −3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370We are required to write a JavaScript function that takes in two numbers, a range, and returns all the numbers between them that are Armstrong numbers (including them, if they are Armstrong).ExampleLet’s write the code for this function −const isArmstrong = number => { let num = number; ... Read More