D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
newsummit
Full window
Github gist
JS Bin [add your bin description] // source https://jsbin.com/heviket
<!DOCTYPE html> <html> <head> <meta name="description" content="Fibonacci"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <script id="jsbin-javascript"> /* The Fibonacci sequence is a sequence of numbers where the first two numbers are defined to be 1 and each subsequent number is the sum of the previous two. 1, 1, 2, 3, 5, 8, 13, 21, 34... Assuming the sequence starts at n=0, write a function that takes an argument 'n' and returns the n-th number in the sequence. */ const fib = { // Recursive solution recursive: function(n) { if (n < 2) { return 1; } return this.recursive(n - 1) + this.recursive(n - 2); }, // Iterative solution iterative: function(n) { if (typeof n !== "number") { throw "n passed to fib() is not a number."; } let arr = [1, 1]; for (let i = 2; i <= n; i++) { arr[i] = arr[i - 1] + arr[i - 2]; } return arr[n]; } } const runFib = (type) => { let result = []; for (let x = 0; x < 10; x++) { result.push(fib[type](x)); } console.log(`${type}: ${result.join(', ')}`); }; runFib('recursive'); runFib('iterative'); </script> <script id="jsbin-source-javascript" type="text/javascript">/* The Fibonacci sequence is a sequence of numbers where the first two numbers are defined to be 1 and each subsequent number is the sum of the previous two. 1, 1, 2, 3, 5, 8, 13, 21, 34... Assuming the sequence starts at n=0, write a function that takes an argument 'n' and returns the n-th number in the sequence. */ const fib = { // Recursive solution recursive: function(n) { if (n < 2) { return 1; } return this.recursive(n - 1) + this.recursive(n - 2); }, // Iterative solution iterative: function(n) { if (typeof n !== "number") { throw "n passed to fib() is not a number."; } let arr = [1, 1]; for (let i = 2; i <= n; i++) { arr[i] = arr[i - 1] + arr[i - 2]; } return arr[n]; } } const runFib = (type) => { let result = []; for (let x = 0; x < 10; x++) { result.push(fib[type](x)); } console.log(`${type}: ${result.join(', ')}`); }; runFib('recursive'); runFib('iterative'); </script></body> </html>