Find the second largest number in a arrayfunction findSecondLargest(arr) { // Sort the array in descending order arr.sort(function(a, b) { return b - a; }); // Return the element at index 1 (the second largest) return arr[1]; } // Example usage const numbers = [10, 5, 8, 20,...Sep 19, 2023·1 min read
JavaScript: Rest operator examplefunction getSumOfNumbers (...numbers) { return [...numbers].reduce ((a, b) => { return a + b; }, 0); } let sumOfNumbers = getSumOfNumbers(1, 2, 3, 4, 5); console.log('Sum Of Numbers:', sumOfNumbers); Output: Sum Of Numbers: 15 Execute above ...Jan 21, 2022·1 min read
JavaScript: Convert last character of each word to uppercasefunction changeLastCharacterToUpperCase (str) { return str.split(" ").map( (item) => { return item.slice(0, -1) + item[item.length - 1].toUpperCase(); }).join (" "); } console.log(changeLastCharacterToUpperCase('hello world welcome testing a...Jan 20, 2022·1 min read
JavaScript: Two sum problemlet startTime, endTime let integerArr = [1, 2, 8, 4, 5, 18]; let targetNum = 19; function twoSumMethod1 (integerArr, targetNum) { let condition = false; for (let i = 0; i < integerArr.length; i++) { for (let j = 0; j < integerArr.length; j++)...Jan 19, 2022·1 min read
JavaScript: How to get the URL parts from a URLconst url = new URL('http://example.com:12345/blog/foo/bar?startIndex=1&pageSize=10'); const { protocol, hostname, port, pathname, search } = url; console.log('protocol =>', protocol); console.log('hostname =>', hostname); console.log('port =>', por...Jan 18, 2022·1 min read
JavaScript: Remove duplicates from array without using any shortcut methodslet arr = [2, 1, 3, 1, 7, 1, 7, 9, 3]; function removeDuplicates(arr) { let obj = {}; let arrWithNoDuplicates = []; for (let i = 0; i < arr.length; i++) { obj[arr[i]] = true; } for (let key in obj) { arrWithNoDup...Jan 17, 2022·1 min read
JavaScript: Group an array according to typeconst array = [ { type: 'A', status:'Approved' }, { type: 'A', status:'Approved' }, { type: 'A', status:'Disapproved' }, { type: 'A', status:'Disapproved' }, { type: 'A', status:'Processing' }, { type: 'B', status:'Processing' }, { type: 'B', status:...Jan 16, 2022·1 min read