- 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)
244 Views
We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string.If there is no such character then we should return -1.ExampleThe code for this will be −const str = 'Hello world, how are you'; const firstRepeating = str => { const map = new Map(); for(let i = 0; i < str.length; i++){ if(map.has(str[i])){ return map.get(str[i]); }; map.set(str[i], i); }; return -1; }; console.log(firstRepeating(str));OutputFollowing is the output on console −2
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
429 Views
We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an odd number of characters in them.Any substring in the string qualifies to be a word, if either it is encapsulated by two spaces on either ends or present at the end or beginning and followed or preceded by a space.ExampleThe code for this will be −const str = 'hello world, how are you'; const idOdd = str => str.length % 2 === 1; const reverseOddWords = (str = '') => { const strArr = str.split(' '); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
174 Views
Suppose, we have an array of numbers like this −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1];We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array.For example:The output for the array mentioned above will be −30ExampleThe code for this will be −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; const distinctSum = arr => { let res = 0; for(let i = 0; i < arr.length; i++){ if(i === arr.lastIndexOf(arr[i])){ res += arr[i]; }; continue; }; return res; }; console.log(distinctSum(arr));OutputFollowing is the output on console −30
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
135 Views
We are required to write a JavaScript function that takes in a number and checks whether it falls in Fibonacci series or not.We should return a boolean on this basis.ExampleThe code for this will be −const num = 89; const isFib = query => { if(query === 0 || query === 1){ return true; } let prev = 1; let count = 2; let temp = 0; while(count >= query){ if(prev + count === query){ return true; }; temp = prev; prev = count; count += temp; }; return false; }; console.log(isFib(num));OutputFollowing is the output on console −true
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
457 Views
Suppose we have a sorted array of literals like this −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9];We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array.If there is no such number in the array, we should return false.For this array, the output should be 6.ExampleThe code for this will be −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; const firstNonDuplicate = arr => { let appeared = false; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
176 Views
We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n.If there exist such numbers, our function should return them, otherwise it should return false.ExampleThe code for this will be −const sum = 54; const threeConsecutiveSum = sum => { if(sum < 6 || sum % 3 !== 0){ return false; } // three numbers will be of the form: // x + x + 1 + ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
233 Views
We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example:If the input string is −const str = "hello";Then the output should be −const output = "ollhe";The code for this will be −const string = 'hello'; const sorter = (a, b) => { const legend = [-1, 0, 1]; return legend[+(a < b)]; } const reverseSort = str => { const strArr = str.split(""); return strArr .sort(sorter) .join(""); }; console.log(reverseSort(string));Following is the output on console −ollhe
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
486 Views
We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.Therefore, the output should look like −const output = [1, 2, 7, 9, 5, 11, 21];Therefore, let’s write the function partialSum(), The full code for this function will be −const arr = [1, 1, 5, 2, -4, 6, 10]; const partialSum = (arr) => { const output = []; arr.forEach((num, index) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number.For examplefindSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6Therefore, the output should be 6.Let’s write the code for this function findSum()// using recursion const findSum = (num) => { if(num < 10){ return num; } const lastDigit = num % 10; const remainingNum = Math.floor(num / 10); return findSum(lastDigit + findSum(remainingNum)); } console.log(findSum(2568));We check if the number is less than 10, it’s already minified and ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
67 Views
We are required to write a JavaScript function that takes in an array of literals. Our function should return a new array with all the triplets filtered.The code for this will be −const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = [1, 1, 1, 1, 3, 3, 5]; const arr3 = [1, 1, 1, 3, 3, 3]; const arr4 = [1, 1, 1, 1, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7]; const removeTriplets = arr => { const hashMap = arr => arr.reduce((acc, val) => { ... Read More