- 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)
318 Views
We are required to write a JavaScript function that takes in a string and returns a new string with only the words that appeared for more than once in the original string.For example:If the input string is −const str = 'this is a is this string that contains that some repeating words';OutputThen the output should be −const output = 'this is that';Let’s write the code for this function −ExampleThe code for this will be −const str = 'this is a is this string that contains that some repeating words'; const keepDuplicateWords = str => { const strArr = str.split(" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
587 Views
The Bucket Sort works by splitting the array of size n into k buckets which holds a specific range of element values.Then, these buckets are then sorted using a sorting algorithm which can be chosen based on the expected input size.We can describe this algorithm as follows −Algorithm:Create the initial bucketSort function Create variables for i, min, max, and bucket size Find min and max value Create amount of buckets Push values to correct buckets Sort bucketsExampleThe code for this will be −const arr = [32, 6, 34, 4, 78, 1, 6767, 4, 65, 34, 879, 7]; const bucketSort = ... 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 takes in two 2-D arrays of numbers and returns their matrix multiplication result.Let’s write the code for this function −ExampleThe code for this will be −const multiplyMatrices = (a, b) => { if (!Array.isArray(a) || !Array.isArray(b) || !a.length || !b.length) { throw new Error('arguments should be in 2-dimensional array format'); } let x = a.length, z = a[0].length, y = b[0].length; if (b.length !== z) { // XxZ & ZxY => XxY throw new Error('number of ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
874 Views
We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters from the original string converted to lowercase and all the lowercase characters converted to uppercase from the original string.For example: If the string is −const str = 'The Case OF tHis StrinG Will Be FLiPped';OutputThen the output should be −const output = 'tHE cASE of ThIS sTRINg wILL bE flIpPED';ExampleThe code for this will be −const str = 'The Case OF tHis StrinG Will Be FLiPped'; const isUpperCase = char => char.charCodeAt(0) >= 65 && char.charCodeAt(0) char.charCodeAt(0) ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
271 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 105.Then the output should be −const output = [3, 5, 7];ExampleThe code for this will be −const num = 105; 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)
87 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.ExampleThe code for this will be −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 −Thisisanexamplestringfromwhichallwhitespaceswillberemoved
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
284 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.QuickSort:This 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)ExampleThe code for this will be −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 other ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Let’s say that we have a string that may contain any of the following characters.'!', "," ,"\'" ,";" ,"\"", ".", "-" ,"?"We are required to write a JavaScript function that takes in a string and count the number of appearances of these characters in the string and return that count.ExampleThe code for this will be −const str = "This, is a-sentence;.Is this a sentence?"; const countSpecial = 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(countSpecial(str));OutputThe output in the console −5
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
123 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 elements.Both the arrays should have same number of rows and columns. Also, arr1[i][j] === arr2[i][j] should yield true for all i between [0, number of rows] and j between [0, number of columns]ExampleThe code for this will be −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)
236 Views
Transpose:The 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.ExampleThe code for this will be −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 ] ]