From e2d2b6aa417b0c1c3c5b78fbdeeafa3e6bae1c8a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 12 Dec 2017 18:21:53 +0200 Subject: [PATCH] Updated snippets Mainly for better readability --- README.md | 13 ++++++------- snippets/initialize-array-with-values.md | 4 ++-- snippets/measure-time-taken-by-function.md | 4 ++-- snippets/object-from-key-value-pairs.md | 2 +- snippets/sum-of-array-of-numbers.md | 3 +-- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5713f6300..701b108ef 100644 --- a/README.md +++ b/README.md @@ -299,10 +299,10 @@ const initializeArrayRange = (end, start = 0) => ### Initialize array with values Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values. -You can omit `v` to use a default value of `0`. +You can omit `value` to use a default value of `0`. ```js -const initializeArray = (n, v = 0) => Array(n).fill(v); +const initializeArray = (n, value = 0) => Array(n).fill(value); // initializeArray(5, 2) -> [2,2,2,2,2] ``` @@ -321,8 +321,8 @@ Use `performance.now()` to get start and end time for the function, `console.log First argument is the function name, subsequent arguments are passed to the function. ```js -const timeTaken = (f,...args) => { - var t0 = performance.now(), r = f(...args); +const timeTaken = (func,...args) => { + var t0 = performance.now(), r = func(...args); console.log(performance.now() - t0); return r; } @@ -334,7 +334,7 @@ const timeTaken = (f,...args) => { Use `Array.reduce()` to create and combine key-value pairs. ```js -const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {}); +const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {}); // objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} ``` @@ -447,8 +447,7 @@ const sortCharactersInString = str => Use `reduce()` to add each value to an accumulator, initialized with a value of `0`. ```js -const sum = arr => - arr.reduce( (acc , val) => acc + val, 0); +const sum = arr => arr.reduce( (acc , val) => acc + val, 0); // sum([1,2,3,4]) -> 10 ``` diff --git a/snippets/initialize-array-with-values.md b/snippets/initialize-array-with-values.md index 3ee75f97c..d8a18110f 100644 --- a/snippets/initialize-array-with-values.md +++ b/snippets/initialize-array-with-values.md @@ -1,9 +1,9 @@ ### Initialize array with values Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values. -You can omit `v` to use a default value of `0`. +You can omit `value` to use a default value of `0`. ```js -const initializeArray = (n, v = 0) => Array(n).fill(v); +const initializeArray = (n, value = 0) => Array(n).fill(value); // initializeArray(5, 2) -> [2,2,2,2,2] ``` diff --git a/snippets/measure-time-taken-by-function.md b/snippets/measure-time-taken-by-function.md index 5f39c34ed..8586f59d1 100644 --- a/snippets/measure-time-taken-by-function.md +++ b/snippets/measure-time-taken-by-function.md @@ -4,8 +4,8 @@ Use `performance.now()` to get start and end time for the function, `console.log First argument is the function name, subsequent arguments are passed to the function. ```js -const timeTaken = (f,...args) => { - var t0 = performance.now(), r = f(...args); +const timeTaken = (func,...args) => { + var t0 = performance.now(), r = func(...args); console.log(performance.now() - t0); return r; } diff --git a/snippets/object-from-key-value-pairs.md b/snippets/object-from-key-value-pairs.md index 0f7a60e44..df01da0ba 100644 --- a/snippets/object-from-key-value-pairs.md +++ b/snippets/object-from-key-value-pairs.md @@ -3,6 +3,6 @@ Use `Array.reduce()` to create and combine key-value pairs. ```js -const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {}); +const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {}); // objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} ``` diff --git a/snippets/sum-of-array-of-numbers.md b/snippets/sum-of-array-of-numbers.md index f2805b08f..fcf01949c 100644 --- a/snippets/sum-of-array-of-numbers.md +++ b/snippets/sum-of-array-of-numbers.md @@ -3,7 +3,6 @@ Use `reduce()` to add each value to an accumulator, initialized with a value of `0`. ```js -const sum = arr => - arr.reduce( (acc , val) => acc + val, 0); +const sum = arr => arr.reduce( (acc , val) => acc + val, 0); // sum([1,2,3,4]) -> 10 ```