harikrishna.hashnode.devFind 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
harikrishna.hashnode.devSleep method to wait before executing the next codeconst sleep = (delay) => { return new Promise((resolve, reject) => { setTimeout(resolve, delay); }); }; async function processData() { console.log("Print immediately") await sleep(5000); console.log("Print after 5 seconds") } processD...Nov 29, 2022·1 min read
harikrishna.hashnode.devMove 0's in the front and non-zeroes at the endconst numbers = [1, 0, 3, 4, 0, 0, 5, true, "js", 2, 0, 7, 0, 9, false, 0]; function moveAllZeroesToFront () { return [...numbers.filter(item => !item && Number(item) === item), ...numbers.filter(item => item !== 0)]; } const arr = moveAllZeroes...Nov 25, 2022·1 min read
harikrishna.hashnode.devGlobal, local variable scope with examplevar age = 30; function getAge () { if ( age === undefined ) { var age = 20; return age; } else { return age; } } console.log(getAge()); Output: 20 Execute above code hereNov 22, 2022·1 min read
harikrishna.hashnode.devGet the current/user time zone without any librariesconst timezone = Intl .DateTimeFormat() .resolvedOptions() .timeZone; console.log('timezone:', timezone); output: timezone: Asia/CalcuttaJan 22, 2022·1 min read