From 7178ef0e315cc88175342409583daf70220b3c87 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Fri, 22 Dec 2017 16:52:25 +0530 Subject: [PATCH 01/10] add snippet fibsTillNum --- snippets/fibsTillNum.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 snippets/fibsTillNum.md diff --git a/snippets/fibsTillNum.md b/snippets/fibsTillNum.md new file mode 100644 index 000000000..c26bc6c01 --- /dev/null +++ b/snippets/fibsTillNum.md @@ -0,0 +1,9 @@ +### fibsTillNum + +Returns the number of fibonnacci numbers till num(0 and num inclusive) + +```js +const fibsTillNum = num => + Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2))// fibonacci(5) -> [0,1,1,2,3] + \\ fibsTillNum(10) -> 7 +``` From 18741354c34b70cad9ba0271ba47806ad2fbb06f Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Fri, 22 Dec 2017 16:58:16 +0530 Subject: [PATCH 02/10] add snippet fibonacciTillNum --- snippets/fibonacciTillNum.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 snippets/fibonacciTillNum.md diff --git a/snippets/fibonacciTillNum.md b/snippets/fibonacciTillNum.md new file mode 100644 index 000000000..252000907 --- /dev/null +++ b/snippets/fibonacciTillNum.md @@ -0,0 +1,14 @@ +### fibonaciiTillNum + +Generates an array, containing the Fibonacci sequence, up until the nth term. + +Create an empty array of the specific length, initializing the first two values (`0` and `1`). +Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two. +Uses a mathematical formula to calculate the length of the array required +```js +const fibonacciTillNum = num => { + let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2))// fibonacci(5) -> [0,1,1,2,3]; + return Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []); + } + \\ fibsTillNum(15) -> [0,1,1,2,3,5,8,13] +``` \ No newline at end of file From 4deb253d4c9e79ad77d32b09c4870b90d18d42cd Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Fri, 22 Dec 2017 18:46:17 +0530 Subject: [PATCH 03/10] make gcd accept variable arguments --- snippets/gcd.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/snippets/gcd.md b/snippets/gcd.md index 34fb21833..bda988f05 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -1,12 +1,16 @@ ### gcd -Calculates the greatest common divisor between two numbers. +Calculates the greatest common divisor between two or more numbers numbers. -Use recursion. +The helper function uses recursion. +The helper case takes two arguments x and y Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js -const gcd = (x, y) => !y ? x : gcd(y, x % y); +const gcd = (...arr) => { + const gcdHelper = (x, y) => !y ? x : gcd(y, x % y); + return arr.reduce((a,b) => gcdHelper(a,b)) +} // gcd (8, 36) -> 4 ``` From 53246ccc6270d7811ae0e0b01762c5f877e6266d Mon Sep 17 00:00:00 2001 From: Soorena Date: Fri, 22 Dec 2017 17:10:30 +0330 Subject: [PATCH 04/10] Update fibonacciTillNum.md resolve issue with comments --- snippets/fibonacciTillNum.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/fibonacciTillNum.md b/snippets/fibonacciTillNum.md index 252000907..63a7f98bb 100644 --- a/snippets/fibonacciTillNum.md +++ b/snippets/fibonacciTillNum.md @@ -10,5 +10,5 @@ const fibonacciTillNum = num => { let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2))// fibonacci(5) -> [0,1,1,2,3]; return Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []); } - \\ fibsTillNum(15) -> [0,1,1,2,3,5,8,13] -``` \ No newline at end of file +// fibsTillNum(15) -> [0,1,1,2,3,5,8,13] +``` From f43ce09e736f2dcea6578ab2777bb883b1670fcc Mon Sep 17 00:00:00 2001 From: Soorena Date: Fri, 22 Dec 2017 17:10:51 +0330 Subject: [PATCH 05/10] Update fibsTillNum.md --- snippets/fibsTillNum.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/fibsTillNum.md b/snippets/fibsTillNum.md index c26bc6c01..a829daee3 100644 --- a/snippets/fibsTillNum.md +++ b/snippets/fibsTillNum.md @@ -5,5 +5,5 @@ Returns the number of fibonnacci numbers till num(0 and num inclusive) ```js const fibsTillNum = num => Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2))// fibonacci(5) -> [0,1,1,2,3] - \\ fibsTillNum(10) -> 7 +// fibsTillNum(10) -> 7 ``` From 099809caad1c1de5a50931cb127e60a3de508223 Mon Sep 17 00:00:00 2001 From: Soorena Date: Fri, 22 Dec 2017 17:11:36 +0330 Subject: [PATCH 06/10] Update fibsTillNum.md --- snippets/fibsTillNum.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/fibsTillNum.md b/snippets/fibsTillNum.md index a829daee3..014bdbbbd 100644 --- a/snippets/fibsTillNum.md +++ b/snippets/fibsTillNum.md @@ -1,9 +1,10 @@ ### fibsTillNum -Returns the number of fibonnacci numbers till num(0 and num inclusive) +Returns the number of fibonnacci numbers till num(0 and num inclusive). ```js const fibsTillNum = num => - Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2))// fibonacci(5) -> [0,1,1,2,3] + Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2)) +// fibonacci(5) -> [0,1,1,2,3] // fibsTillNum(10) -> 7 ``` From 74d043b8679a3bb2f06a26060644eb526e6e0bac Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Fri, 22 Dec 2017 22:44:51 +0530 Subject: [PATCH 07/10] add snippet toKebabCase --- snippets/arrayMin.md | 2 +- snippets/arraySum.md | 2 +- snippets/gcd.md | 10 +++------- snippets/toKebabCase.md | 17 +++++++++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 snippets/toKebabCase.md diff --git a/snippets/arrayMin.md b/snippets/arrayMin.md index 5bdd95559..6df9b799a 100644 --- a/snippets/arrayMin.md +++ b/snippets/arrayMin.md @@ -5,6 +5,6 @@ Returns the minimum value in an array. Use `Math.min()` combined with the spread operator (`...`) to get the minimum value in the array. ```js -const arrayMin = arr => Math.min(...arr); +const arrayMin = zzarr => Math.min(...arr); // arrayMin([10, 1, 5]) -> 1 ``` diff --git a/snippets/arraySum.md b/snippets/arraySum.md index 265c7fc60..31533d929 100644 --- a/snippets/arraySum.md +++ b/snippets/arraySum.md @@ -5,6 +5,6 @@ Returns the sum of an array of numbers. Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`. ```js -const arraySum = arr => arr.reduce((acc, val) => acc + val, 0); +const arraySum = zarr => arr.reduce((acc, val) => acc + val, 0); // arraySum([1,2,3,4]) -> 10 ``` diff --git a/snippets/gcd.md b/snippets/gcd.md index bda988f05..34fb21833 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -1,16 +1,12 @@ ### gcd -Calculates the greatest common divisor between two or more numbers numbers. +Calculates the greatest common divisor between two numbers. -The helper function uses recursion. -The helper case takes two arguments x and y +Use recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js -const gcd = (...arr) => { - const gcdHelper = (x, y) => !y ? x : gcd(y, x % y); - return arr.reduce((a,b) => gcdHelper(a,b)) -} +const gcd = (x, y) => !y ? x : gcd(y, x % y); // gcd (8, 36) -> 4 ``` diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md new file mode 100644 index 000000000..9d5432325 --- /dev/null +++ b/snippets/toKebabCase.md @@ -0,0 +1,17 @@ +### toKebabCase + +Converts a string to snakecase. +Use `replace()` to add spaces before capital letters, convert `toLowerCase()`, then `replace()` and undesrsocres and spaces with hyphens. +Also check if a string starts with hyphen and if yes remove it. + +```js +const toKebabCase = str => { + str = str.replace(/([A-Z])/g," $1").toLowerCase().replace(/_/g,' ').replace(/\s\s+/g, '-').replace(/\s/g,'-'); + return str.startsWith('-') ? str.slice(1,str.length) : str; +} +// toKebabCase("camelCase") -> 'camel-case' +// toKebabCase("some text") -> 'some-text' +// toKebabCase("some-javascript-property") -> 'some-javascript-property' +// toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some-mixed-string-with-spaces-underscores-and-hyphens' +// toKebabCase("AllThe-small Things") -> "all-the-small-things" +``` From c5ca1393b8628e658bf36bb6cee6931fa6b914f1 Mon Sep 17 00:00:00 2001 From: Rohit Date: Sat, 23 Dec 2017 08:52:08 +0530 Subject: [PATCH 08/10] remove toKebabCase --- snippets/toKebabCase.md | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 snippets/toKebabCase.md diff --git a/snippets/toKebabCase.md b/snippets/toKebabCase.md deleted file mode 100644 index 9d5432325..000000000 --- a/snippets/toKebabCase.md +++ /dev/null @@ -1,17 +0,0 @@ -### toKebabCase - -Converts a string to snakecase. -Use `replace()` to add spaces before capital letters, convert `toLowerCase()`, then `replace()` and undesrsocres and spaces with hyphens. -Also check if a string starts with hyphen and if yes remove it. - -```js -const toKebabCase = str => { - str = str.replace(/([A-Z])/g," $1").toLowerCase().replace(/_/g,' ').replace(/\s\s+/g, '-').replace(/\s/g,'-'); - return str.startsWith('-') ? str.slice(1,str.length) : str; -} -// toKebabCase("camelCase") -> 'camel-case' -// toKebabCase("some text") -> 'some-text' -// toKebabCase("some-javascript-property") -> 'some-javascript-property' -// toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some-mixed-string-with-spaces-underscores-and-hyphens' -// toKebabCase("AllThe-small Things") -> "all-the-small-things" -``` From 052407cbeb8dd31ef1050450c934a184fa79f809 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 23 Dec 2017 12:11:31 +0200 Subject: [PATCH 09/10] Update and rename fibonacciTillNum.md to fibonacciUntilNum.md --- .../{fibonacciTillNum.md => fibonacciUntilNum.md} | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) rename snippets/{fibonacciTillNum.md => fibonacciUntilNum.md} (76%) diff --git a/snippets/fibonacciTillNum.md b/snippets/fibonacciUntilNum.md similarity index 76% rename from snippets/fibonacciTillNum.md rename to snippets/fibonacciUntilNum.md index 63a7f98bb..55df575d0 100644 --- a/snippets/fibonacciTillNum.md +++ b/snippets/fibonacciUntilNum.md @@ -1,14 +1,15 @@ -### fibonaciiTillNum +### fibonacciUntilNum Generates an array, containing the Fibonacci sequence, up until the nth term. Create an empty array of the specific length, initializing the first two values (`0` and `1`). Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two. -Uses a mathematical formula to calculate the length of the array required +Uses a mathematical formula to calculate the length of the array required. + ```js -const fibonacciTillNum = num => { - let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2))// fibonacci(5) -> [0,1,1,2,3]; +const fibonacciUntilNum = num => { + let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2)); return Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []); - } -// fibsTillNum(15) -> [0,1,1,2,3,5,8,13] +} +// fibonacciUntilNum(15) -> [0,1,1,2,3,5,8,13] ``` From 99b5fce9c7e8df6655589a520f94a25c3314a331 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 23 Dec 2017 12:13:21 +0200 Subject: [PATCH 10/10] Update and rename fibsTillNum.md to fibonacciCountUntilNum.md --- snippets/fibonacciCountUntilNum.md | 11 +++++++++++ snippets/fibsTillNum.md | 10 ---------- 2 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 snippets/fibonacciCountUntilNum.md delete mode 100644 snippets/fibsTillNum.md diff --git a/snippets/fibonacciCountUntilNum.md b/snippets/fibonacciCountUntilNum.md new file mode 100644 index 000000000..12fffbfb6 --- /dev/null +++ b/snippets/fibonacciCountUntilNum.md @@ -0,0 +1,11 @@ +### fibonacciCountUntilNum + +Returns the number of fibonnacci numbers up to `num`(`0` and `num` inclusive). + +Use a mathematical formula to calculate the number of fibonacci numbers until `num`. + +```js +const fibonacciCountUntilNum = num => + Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2)); +// fibonacciCountUntilNum(10) -> 7 +``` diff --git a/snippets/fibsTillNum.md b/snippets/fibsTillNum.md deleted file mode 100644 index 014bdbbbd..000000000 --- a/snippets/fibsTillNum.md +++ /dev/null @@ -1,10 +0,0 @@ -### fibsTillNum - -Returns the number of fibonnacci numbers till num(0 and num inclusive). - -```js -const fibsTillNum = num => - Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2)) -// fibonacci(5) -> [0,1,1,2,3] -// fibsTillNum(10) -> 7 -```