Created
July 21, 2017 08:10
-
-
Save josephholsten/32158b5fca0008b477058cad70408cf8 to your computer and use it in GitHub Desktop.
Fibonacci for Webtask.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request') | |
// Fib takes an integer from the query param q, recursively calculates the fibonacci function and returns its value | |
var Fib = function (context, callback) { | |
// apiURI should be set to the URI for this webtask | |
var api = context.secrets.apiURI | |
// fib(x) is provided by the query param "q" | |
var x = parseInt(context.data.q) | |
switch (x) { | |
case 0: | |
callback(null, {y: 0}) | |
break | |
case 1: | |
callback(null, {y: 1}) | |
break | |
default: | |
request({uri: api + '?q=' + (x - 1), json: true}, function (error, res, body) { | |
if (error) { | |
callback(error) | |
} else { | |
var y1 = body.y | |
request({uri: api + '?q=' + (x - 2), json: true}, function (error, res, body) { | |
if (error) { | |
callback(error) | |
} else { | |
var y2 = body.y | |
callback(null, {y: y1 + y2}) | |
} | |
}) | |
} | |
}) | |
break | |
} | |
} | |
module.exports = Fib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment