Skip to content

Instantly share code, notes, and snippets.

@edsadr

edsadr/answer.js Secret

Created September 30, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save edsadr/4318c279497c75c0ec1fed1812e24a9f to your computer and use it in GitHub Desktop.
Save edsadr/4318c279497c75c0ec1fed1812e24a9f to your computer and use it in GitHub Desktop.
NCD Workshop homework - Task 7.1

Task 7.1

The answer.js file contains the following:

'use strict'

const {
  PORT
} = process.env

function fibonacci (num) {
  let a = 1
  let b = 0
  let temp

  while (num >= 0) {
    temp = a
    a = a + b
    b = temp
    num--
  }

  return b
}

Using the Node-core API's or any framework or libraries of your choice, build a web-server listening for requests on PORT.

This server should have a single endpoint /calculate-fibonacci responding to GET, returning 404 for other routes and methods.

/calculate-fibonacci should merely allow an URL query parameter called digit, and respond to these codes managing the following scenarios:

  • if digit is missing - status code 400
  • if extra URL query parameters are received - 406
  • if digit is not an integer - 400
  • if digit is greater than 50 - 413

If any of the exceptions above occur, The endpoint should return the result of the fibonacci function for digit with a status code of 200 along with a json result, in the following format:

{
  "digit": original digit here,
  "result": result of the fibonacci function here
}
'use strict'
const {
PORT
} = process.env
function fibonacci (num) {
let a = 1
let b = 0
let temp
while (num >= 0) {
temp = a
a = a + b
b = temp
num--
}
return b
}
{
"name": "submission",
"version": "1.0.0",
"description": "",
"main": "answer.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "UNLICENSED",
"dependencies": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment