From bc042c848f83ee1a78eb0583f455331142be2910 Mon Sep 17 00:00:00 2001 From: Lakshman Gope Date: Tue, 26 Mar 2024 23:08:26 +0600 Subject: [PATCH 1/2] Create fetchURLsWithDelay.js Call a function where two (2) URLs in an array variable and fetch these URLs with a gap of 1 second --- js-coding-technique/fetchURLsWithDelay.js | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 js-coding-technique/fetchURLsWithDelay.js diff --git a/js-coding-technique/fetchURLsWithDelay.js b/js-coding-technique/fetchURLsWithDelay.js new file mode 100644 index 0000000..afb3d89 --- /dev/null +++ b/js-coding-technique/fetchURLsWithDelay.js @@ -0,0 +1,31 @@ +function fetchURLsWithDelay(urls) { + let index = 0; + + function fetchNext() { + if (index < urls.length) { + fetch(urls[index]) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); + }) + .then(data => { + console.log('Data fetched:', data); + index++; + setTimeout(fetchNext, 1000); // 1000 milliseconds = 1 second + }) + .catch(error => { + console.error('Error fetching data:', error); + index++; + setTimeout(fetchNext, 1000); // Move to next URL even if there's an error + }); + } + } + + fetchNext(); +} + +// Example usage: +const urls = ['https://example.com/url1', 'https://example.com/url2']; +fetchURLsWithDelay(urls); From f0faa036b69a3dd2d6d069a9d4ec7bc607430aa8 Mon Sep 17 00:00:00 2001 From: Lakshman Gope Date: Sat, 22 Jun 2024 11:03:11 +0600 Subject: [PATCH 2/2] Update README.md --- README.md | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 888e422..871b750 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 1. [Important Methods](#methods) ## Methods -> Most important javascript build in methods +> Hello Javascript - [1.1](#typeof) **typeof**: Returns the type. @@ -118,8 +118,30 @@ console.log(user); // TypeError: Cannot assign to read only property 'age' of ob ``` -- [1.5](#rename) **rename**: Rename multiple files extentions at once by a command (Just for Win). +- [1.7](#rename) **rename**: Rename multiple files extentions at once by a command (Just for Win). ```javascript Get-ChildItem *.css | Rename-Item -NewName { $_.name -Replace '\.css','.scss' } ``` + + +- [1.8](#majority) **majority**: Find Majority Element. + +```javascript +function majorityElement(arr) { + let count = 0, candidate = null; + + for (let num of arr) { + if (count === 0) candidate = num; + count += (num === candidate) ? 1 : -1; + } + + return candidate; +} + +// Time complexity: O(n) +// Space complexity: O(1) + +const arr = [3, 2, 3, 4, 3, 1, 6, 6, 7, 8, 6, 9, 6]; +console.log(majorityElement(arr)); // Output: 6 +```