From 1a6804f988a56b771ebcd5561f32774685722249 Mon Sep 17 00:00:00 2001 From: Elder Henrique Souza Date: Tue, 12 Dec 2017 14:45:24 -0200 Subject: [PATCH 1/2] Update fibonacci-array-generator.md The fibonacci sequence being zero indexed as well as the arrays, I think we could just initialize an array with the passed length, fill it with zeroes and use the indexes for the first positions. --- snippets/fibonacci-array-generator.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/snippets/fibonacci-array-generator.md b/snippets/fibonacci-array-generator.md index ef6a2a975..38cfbc943 100644 --- a/snippets/fibonacci-array-generator.md +++ b/snippets/fibonacci-array-generator.md @@ -4,11 +4,13 @@ Create an empty array of the specific length, initializing the first two values Use `reduce()` to add values into the array, using the sum of the last two values, except for the first two. ```js -const 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); +const fibonacci = n => { + return Array(n) + .fill(0) + .reduce((acc, val, i) => { + acc.push(i > 1 ? acc[i - 1] + acc[i - 2] : i); return acc; },[]); +} // fibonacci(5) -> [0,1,1,2,3] ``` From a69bd30660d5309ac46e8a146cacddcb8ffec872 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 12 Dec 2017 19:20:56 +0200 Subject: [PATCH 2/2] Update fibonacci-array-generator.md --- snippets/fibonacci-array-generator.md | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/snippets/fibonacci-array-generator.md b/snippets/fibonacci-array-generator.md index 38cfbc943..0255e8875 100644 --- a/snippets/fibonacci-array-generator.md +++ b/snippets/fibonacci-array-generator.md @@ -1,16 +1,10 @@ ### 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. +Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two. ```js -const fibonacci = n => { - return Array(n) - .fill(0) - .reduce((acc, val, i) => { - acc.push(i > 1 ? acc[i - 1] + acc[i - 2] : i); - return acc; - },[]); -} +const fibonacci = n => + Array(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),[]); // fibonacci(5) -> [0,1,1,2,3] ```