![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 9326 Articles for Object Oriented Programming
![Nikitasha Shrivastava](https://www.tutorialspoint.com/assets/profiles/655298/profile/60_3988199-1684213136.jpeg)
680 Views
In our problem statement we have to sort odd and even elements separately with the help of Javascript functionalities. So for doing this task we will use for loops and bubble sort for sorting odd and even numbers separately. Understanding the problem statement The problem statement is to write a Javascript function that will help to sort odd and even numbers in a given array. For example if we have an array [2, 3, 5, 4] then we will first sort even indexed elements [3, 4] and then sort the odd indexed elements in the array [2, 5]. After sorting ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
893 Views
We are required to write a JavaScript function that takes in a string containing spaces. The function should simply count the number of spaces present in that string.For example −If the input string is −const str = 'this is a string';Then the output should be −const output = 4;Exampleconst str = 'this is a string'; const countSpaces = (str = '') => { let count = 0; for(let i = 0; i < str.length; i++){ const el = str[i]; if(el !== ' '){ continue; }; count++; }; return count; }; console.log(countSpaces(str));OutputThis will produce the following output −4
![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 that might contain some repeating numbers.The function should sort the array such that the elements that are repeated for the least number of times appears first followed by the elements with increasing frequency.For example −If the input array is −const arr = [1, 1, 2, 2, 2, 3];Then the sorted array should be −const output = [3, 1, 1, 2, 2, 2];Exampleconst arr = [1, 1, 2, 2, 2, 3]; const frequencySort = (arr = []) => { let map = {}; for ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
141 Views
We are given an array of distinct integers, let’s say arr, and another array of integer arrays, let say sourceArr.In the sourceArr array, the integers are distinct. We should write a function that forms arr by concatenating the arrays in sourceArr in any order.However, we cannot reorder the integers inside of any subarray in the soureArr. We should return true if it is possible to form the array arr from sourceArr, false otherwise.For example −const arr = [23, 67, 789]; const sourceArr = [[23], [789, 67]];The function should return false because we cannot reorder the elements inside a subarray and ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
737 Views
We are required to write a JavaScript function that can compare two strings and return the percentage likeliness of how much they are alike. The percentage will be nothing but a measure of many characters the two strings have in common.If they are completely similar the output should be 100, and if they contain no common character at all, the output should be 0.Exampleconst calculateSimilarity = (str1 = '', str2 = '') => { let longer = str1; let shorter = str2; if (str1.length < str2.length) { longer = str2; shorter = str1; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
473 Views
We are required to write a JavaScript function that takes in multiple arrays of numbers. The function should return an array of the cartesian product of the elements from all the arrays.For example −If the input arrays are −[1, 2], [10, 20], [100, 200, 300]Then the output should be −const output = [ [ 1, 10, 100 ], [ 1, 10, 200 ], [ 1, 10, 300 ], [ 1, 20, 100 ], [ 1, 20, 200 ], [ 1, 20, 300 ], [ 2, 10, 100 ], [ 2, 10, 200 ], [ 2, 10, 300 ], [ 2, 20, 100 ], [ 2, 20, 200 ], [ 2, 20, 300 ] ];Exampleconst arr1 = [1, 2]; const arr2 = [10, 20]; const arr3 = [100, 200, 300]; const cartesianProduct = (...arr) => { return arr.reduce((acc,val) => { return acc.map(el => { return val.map(element => { return el.concat([element]); }); }).reduce((acc,val) => acc.concat(val) ,[]); }, [[]]); }; console.log(cartesianProduct(arr1, arr2, arr3));OutputThis will produce the following output −[ [ 1, 10, 100 ], [ 1, 10, 200 ], [ 1, 10, 300 ], [ 1, 20, 100 ], [ 1, 20, 200 ], [ 1, 20, 300 ], [ 2, 10, 100 ], [ 2, 10, 200 ], [ 2, 10, 300 ], [ 2, 20, 100 ], [ 2, 20, 200 ], [ 2, 20, 300 ] ]
![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 a string. It should return true or false based on whether the input consists of a repeated character sequence.The length of the given string is always greater than 1 and the character sequence must have at least one repetition.For example −"aa" should return true because it entirely contains two strings "a""aaa" should return true because it entirely contains three string "a""abcabcabc" should return true because it entirely containas three strings "abc""aba" should return false because it at least there should be two same substrings and nothing more"ababa" should return ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
962 Views
We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a single number as the second argument.The function should push the number specified as the second argument into the array without distorting the sorting of the elements.We are required to do this without creating another array.Exampleconst arr = [6, 7, 8, 9, 12, 14, 16, 17, 19, 20, 22]; const num = 15; const findIndex = (arr, val) => { let low = 0, high = arr.length; while (low < high) { let mid ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
389 Views
We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array of numbers on the following criteria −---If the array contains any zeros, they should all appear in the beginning.---All the remaining numbers should be placed in a decreasing order.For example −If the input array is −const arr = [4, 7, 0 ,3, 5, 1, 0];Then after applying the sort, the array should become −const output = [0, 0, 7, 5, 4, 3, 1];We will use the Array.prototype.sort() method here.For the decreasing order sort, we will take the difference of ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
387 Views
We are required to write a JavaScript function that takes in a number. The function should first calculate the factorial of that number and then it should return the sum of the digits of the calculated factorial.For example −For the number 6, the factorial will be 720, so the output should be 9Exampleconst factorial = (num) => { if (num == 1) return 1; return num * factorial(num-1); }; const sumOfDigits = (num = 1) => { const str = num.toString(); let sum = 0; for (var x = -1; ++x < str.length;) { sum += +str[x]; }; return sum; }; const sumFactorialDigits = num => sumOfDigits(factorial(num)); console.log(sumFactorialDigits(6));OutputThis will produce the following output −9