From da81661a48796b8038c0280ffb0b45d3c35e21c3 Mon Sep 17 00:00:00 2001 From: Nishanth2143 Date: Sun, 11 Oct 2020 11:48:16 +0530 Subject: [PATCH 1/5] check whether string is an isogram or not --- snippets/isIsogram.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 snippets/isIsogram.md diff --git a/snippets/isIsogram.md b/snippets/isIsogram.md new file mode 100644 index 000000000..e26e7683b --- /dev/null +++ b/snippets/isIsogram.md @@ -0,0 +1,26 @@ +--- +title: isIsogram +tags: string, intermediate +--- + +Creates a function that accepts a string parameter and output whether its an isogram or not. + +- Use `String.toLowerCase()` to convert the string to lowercase letters. +- Use `String.prototype.split()`, `String.prototype.indexOf()` and `Array.prototype.every()` to split the string into substrings, run a test for all the elements in that array and finally return the index of first occurence of a specified value in a string. +- Function returns true if the string is an isogram (that is no letter is repeated) else returns false. +- Returns false is called with no parameter. + +```js +const isIsogram = (str = null) => { + if(str == null) return false; + str = str.toLowerCase(); + return str.split("").every((c, i) => str.indexOf(c) === i); +} +``` + +```js +isIsogram("Dermatoglyphics"); // true +isIsogram("aba"); // false +isIsogram("moOse"); // false +isIsogram(); // false +``` From cb394a6c790673e6e67c35ea87cfe38f403b3d0e Mon Sep 17 00:00:00 2001 From: nishanth2143 Date: Sun, 11 Oct 2020 11:49:50 +0530 Subject: [PATCH 2/5] Update isIsogram.md --- snippets/isIsogram.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/isIsogram.md b/snippets/isIsogram.md index e26e7683b..ba554c229 100644 --- a/snippets/isIsogram.md +++ b/snippets/isIsogram.md @@ -8,7 +8,7 @@ Creates a function that accepts a string parameter and output whether its an iso - Use `String.toLowerCase()` to convert the string to lowercase letters. - Use `String.prototype.split()`, `String.prototype.indexOf()` and `Array.prototype.every()` to split the string into substrings, run a test for all the elements in that array and finally return the index of first occurence of a specified value in a string. - Function returns true if the string is an isogram (that is no letter is repeated) else returns false. -- Returns false is called with no parameter. +- Returns false if called with no parameter. ```js const isIsogram = (str = null) => { From f3012a3bfb9c10d18644230e360ac21c4ea1af24 Mon Sep 17 00:00:00 2001 From: Nishanth2143 Date: Sun, 11 Oct 2020 14:14:44 +0530 Subject: [PATCH 3/5] countWeekDaysBetween --- snippets/countWeekDaysBetween.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 snippets/countWeekDaysBetween.md diff --git a/snippets/countWeekDaysBetween.md b/snippets/countWeekDaysBetween.md new file mode 100644 index 000000000..4730f6198 --- /dev/null +++ b/snippets/countWeekDaysBetween.md @@ -0,0 +1,27 @@ +--- +title: countWeekDaysBetween +tags: date, intermediate +--- + +Returns the weekdays count between two dates. + +- Takes startDate and endDate as inputs. +- Executes a while loop if startDate is less than endDate and increments count if startDate does not fall on a weekend. +- Adds one day to startDate in an iteration and the loop exits when startDate becomes equal to endDate. + +```js +const countWeekDaysBetween = (startDate, endDate) => { + let count = 0; + while (startDate < endDate) { + if (startDate.getDay() !== 0 && startDate.getDay() !== 6) { + count++; + } + startDate = new Date(startDate.setDate(startDate.getDate() + 1)); + } + return count; +} +``` + +```js +countWeekDaysBetween(new Date("Oct 09, 2020"), new Date("Oct 14, 2020")); // 3 +``` From ad79909c2e24660dd5420798040516a0b2843042 Mon Sep 17 00:00:00 2001 From: Nishanth2143 Date: Sun, 11 Oct 2020 14:16:18 +0530 Subject: [PATCH 4/5] removed isogram --- snippets/isIsogram.md | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 snippets/isIsogram.md diff --git a/snippets/isIsogram.md b/snippets/isIsogram.md deleted file mode 100644 index ba554c229..000000000 --- a/snippets/isIsogram.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -title: isIsogram -tags: string, intermediate ---- - -Creates a function that accepts a string parameter and output whether its an isogram or not. - -- Use `String.toLowerCase()` to convert the string to lowercase letters. -- Use `String.prototype.split()`, `String.prototype.indexOf()` and `Array.prototype.every()` to split the string into substrings, run a test for all the elements in that array and finally return the index of first occurence of a specified value in a string. -- Function returns true if the string is an isogram (that is no letter is repeated) else returns false. -- Returns false if called with no parameter. - -```js -const isIsogram = (str = null) => { - if(str == null) return false; - str = str.toLowerCase(); - return str.split("").every((c, i) => str.indexOf(c) === i); -} -``` - -```js -isIsogram("Dermatoglyphics"); // true -isIsogram("aba"); // false -isIsogram("moOse"); // false -isIsogram(); // false -``` From 5a6bb0c353abd3a1f22d5d36a421ea20ff6dc1f7 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 11 Oct 2020 14:33:23 +0300 Subject: [PATCH 5/5] Update countWeekDaysBetween.md --- snippets/countWeekDaysBetween.md | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/snippets/countWeekDaysBetween.md b/snippets/countWeekDaysBetween.md index 4730f6198..5bdb770d3 100644 --- a/snippets/countWeekDaysBetween.md +++ b/snippets/countWeekDaysBetween.md @@ -1,27 +1,26 @@ --- title: countWeekDaysBetween -tags: date, intermediate +tags: date,array,intermediate --- -Returns the weekdays count between two dates. +Returns the weekday count between two dates. -- Takes startDate and endDate as inputs. -- Executes a while loop if startDate is less than endDate and increments count if startDate does not fall on a weekend. -- Adds one day to startDate in an iteration and the loop exits when startDate becomes equal to endDate. +- Use `Array.from()` to construct an array with `length` equal to the number of days between `startDate` and `endDate`. +- Use `Array.prototype.reduce()` to iterate over the array, checking if each date is a weekday and incrementing `count`. +- Update `startDate` with the next day each loop using `Date.getDate()` and `Date.setDate()` to advance it by one day. ```js -const countWeekDaysBetween = (startDate, endDate) => { - let count = 0; - while (startDate < endDate) { - if (startDate.getDay() !== 0 && startDate.getDay() !== 6) { - count++; - } - startDate = new Date(startDate.setDate(startDate.getDate() + 1)); - } - return count; -} +const countWeekDaysBetween = (startDate, endDate) => + Array + .from({ length: (endDate - startDate) / (1000 * 3600 * 24) }) + .reduce(count => { + if (startDate.getDay() % 6 !== 0) count++; + startDate = new Date(startDate.setDate(startDate.getDate() + 1)); + return count; + }, 0); ``` ```js -countWeekDaysBetween(new Date("Oct 09, 2020"), new Date("Oct 14, 2020")); // 3 +countWeekDaysBetween(new Date('Oct 05, 2020'), new Date('Oct 06, 2020')); // 1 +countWeekDaysBetween(new Date('Oct 05, 2020'), new Date('Oct 14, 2020')); // 7 ```