Skip to content

Instantly share code, notes, and snippets.

@sveisvei
Created October 19, 2011 18:15
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 sveisvei/1299173 to your computer and use it in GitHub Desktop.
Save sveisvei/1299173 to your computer and use it in GitHub Desktop.
express = require("express")
app = module.exports = express.createServer()
app.configure ->
app.use express.bodyParser()
app.use app.router
ans =
tallErStorst:
rex: /^(.*)\: hvilket av disse tallene er storst\:\s(.*)/,
compute: (match, cb) ->
##console.log("COMPUTE", match)
asd = match[2].replace(/^\s/).split(",")
asd.sort (a, b) -> (1*a) < (1*b)
cb(asd[0] + "")
tallPlussTall:
rex: /^(.*)\: hva er (\d+) pluss (\d+)$/
compute: (match, cb) ->
cb( ((match[2] * 1) + ( match[3] * 1)) + "")
tallPlussTallGangerMed:
rex: /^(.*)\: hva er (\d+) pluss (\d+) ganget med (\d+)$/
compute: (match, cb) ->
cb( (((match[2] * 1) + ( match[3] * 1)) *(match[4])) + "")
tallMinus:
rex: /^(.*)\: hva er (\d+) minus (\d+)/
compute: (match, cb) ->
cb( ((match[2] * 1) - ( match[3] * 1)) + "")
produkter:
rex: /^(.*)\: hvilke produkter har du til salgs \(kommaseparert\)/
compute: (match, cb) ->
cb("Olga, Snelga, Helga")
pris:
rex: /^(.*)\: hvor mange kroner koster ett stykk (\S+)$/
compute: (match, cb) ->
if (match[2] == "Olga")
cb("2000")
else if (match[2] == "Snelga")
cb("3000")
else if (match[2] == "Helga")
cb("4000")
else
cb("0")
ganger:
rex: /^(.*)\: hva er (\d+) ganget med (\d+)$/
compute: (match, cb) ->
cb(match[2] * match[3] + "")
gangerPluss:
rex: /^(.*)\: hva er (\d+) ganget med (\d+) pluss (\d+)/
compute: (match, cb) ->
cb(((match[2] * match[3]) + (match[4]*1))+ "")
delt:
rex: /^(.*)\: hva er (\d+) delt med (\d+)/
compute: (match, cb) ->
cb((match[2] / match[3]) + "")
firma:
rex: /^(.*)\: hvilket firma jobber Magnar og Kjetil i/
compute: (match, cb) ->
cb('Kodemaker')
navn:
rex: /^(.*)\: mitt navn er ([a-zA-Z]+). hva heter jeg/
compute: (match, cb) ->
cb(match[2])
fib:
rex: /^(.*)\: hva er det (\d+)\. nummeret i Fibonaccirekken/
compute: (match, cb) ->
cb(fibMe(match[2]))
fibMe = (fibTo, i = 0, a = 0, b = 1) ->
newB = a + b
return a if i == fibTo
fibMe(fibTo, i + 1, b, newB)
quest = (question, cb) ->
resultComputed = false;
for obj of ans
OBJ = ans[obj]
match = question.match(OBJ.rex)
if (match and match.length > 0)
OBJ.compute(match, cb)
resultComputed = true
if not resultComputed
cb("MISS")
app.get "*", (req, res) ->
return res.send "undefined" if not req.query.q?
quest req.query.q, (result) ->
console.log("SENDING RESULT", req.query.q, result)
res.send result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment