From 0893f79360202242c05ff52985a3e17a01d08d9c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 30 Nov 2017 19:45:56 +0200 Subject: [PATCH] Fibonacci and random --- README.md | 37 +++++++++++++++++++++++++-- snippets/fibonacci-array-generator.md | 13 ++++++++++ snippets/random-number-in-range.md | 7 +++++ snippets/randomize-order-of-array.md | 7 +++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 snippets/fibonacci-array-generator.md create mode 100644 snippets/random-number-in-range.md create mode 100644 snippets/randomize-order-of-array.md diff --git a/README.md b/README.md index c64fd9b20..2c8958c04 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,10 @@ ## Contents * [Even or odd number](#even-or-odd-number) +* [Fibonacci array generator](#fibonacci-array-generator) * [Greatest common divisor (GCD)](#greatest-common-divisor-gcd) +* [Random number in range](#random-number-in-range) +* [Randomize order of array](#randomize-order-of-array) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Sort characters in string alphabetical](#sort-characters-in-string-alphabetical) * [Sum of array of numbers](#sum-of-array-of-numbers) @@ -24,7 +27,21 @@ Return `true` if the number is even, `false` if the number is odd. var isEven = num => Math.abs(num) % 2 === 0; ``` -## Greatest common divisor (GCD) +### Fibonacci array generator + +Create an empty array of the specific length, initializing the first two values (`0` and `1`). +Use `reduce()` to add values into the array, using the sum of the last two values, except for the first two. + +```js +var fibonacci = n => + Array.apply(null, [0,1].concat(Array(n-2))).reduce( + (acc, val, i) => { + acc.push( i>1 ? acc[i-1]+acc[i-2] : val); + return acc; + },[]); +``` + +### Greatest common divisor (GCD) Use recursion. Base case is when `y` equals `0`. In this case, return `x`. @@ -34,7 +51,23 @@ Otherwise, return the GCD of `y` and the remainder of the division `x/y`. var gcd = (x , y) => !y ? x : gcd(y, x % y); ``` -## RGB to hexadecimal +### Random number in range + +Use `Math.random()` to generate a random value, map it to the desired range using multiplication. + +```js +var randomInRange = (min, max) => Math.random() * (max - min) + min; +``` + +### Randomize order of array + +Use `sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting. + +```js +var randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1) +``` + +### RGB to hexadecimal Convert each value to a hexadecimal string, using `toString(16)`, then `padStart(2,'0')` to get a 2-digit hexadecimal value. Combine values using `join('')`. diff --git a/snippets/fibonacci-array-generator.md b/snippets/fibonacci-array-generator.md new file mode 100644 index 000000000..209b186d6 --- /dev/null +++ b/snippets/fibonacci-array-generator.md @@ -0,0 +1,13 @@ +### Fibonacci array generator + +Create an empty array of the specific length, initializing the first two values (`0` and `1`). +Use `reduce()` to add values into the array, using the sum of the last two values, except for the first two. + +```js +var fibonacci = n => + Array.apply(null, [0,1].concat(Array(n-2))).reduce( + (acc, val, i) => { + acc.push( i>1 ? acc[i-1]+acc[i-2] : val); + return acc; + },[]); +``` diff --git a/snippets/random-number-in-range.md b/snippets/random-number-in-range.md new file mode 100644 index 000000000..f9f768fa0 --- /dev/null +++ b/snippets/random-number-in-range.md @@ -0,0 +1,7 @@ +### Random number in range + +Use `Math.random()` to generate a random value, map it to the desired range using multiplication. + +```js +var randomInRange = (min, max) => Math.random() * (max - min) + min; +``` diff --git a/snippets/randomize-order-of-array.md b/snippets/randomize-order-of-array.md new file mode 100644 index 000000000..8228dcfdf --- /dev/null +++ b/snippets/randomize-order-of-array.md @@ -0,0 +1,7 @@ +### Randomize order of array + +Use `sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting. + +```js +var randomizeOrder = arr => arr.sort( (a,b) => Math.random() >= 0.5 ? -1 : 1) +```