Skip to content

Instantly share code, notes, and snippets.

@josephholsten
Created July 21, 2017 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephholsten/32158b5fca0008b477058cad70408cf8 to your computer and use it in GitHub Desktop.
Save josephholsten/32158b5fca0008b477058cad70408cf8 to your computer and use it in GitHub Desktop.
Fibonacci for Webtask.io
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