Skip to content

Instantly share code, notes, and snippets.

@mastoj
Created February 28, 2013 21:15
Show Gist options
  • Save mastoj/5060181 to your computer and use it in GitHub Desktop.
Save mastoj/5060181 to your computer and use it in GitHub Desktop.
Coding dojo. The most interesting parts are from row 123 to 149.
var express = require("express");
/* Reimplement this function to answer questions. */
var myName = function(matches) {
return "Tomas";
};
var yourName = function(matches) {
return matches[1];
};
var compareNumbers = function(matches) {
var num1 = parseInt(matches[1]);
var num2 = parseInt(matches[2]);
if(num2 >= num1) { return num2; }
return num1+ "";
};
var compareNumbers2 = function(matches) {
var numbers = matches[1].split(",");
var largestNumber = parseInt(numbers[0].trim());
for (var i = 1; i < numbers.length; i++) {
var candidate = parseInt(numbers[i].trim());
console.log(candidate);
if(candidate > largestNumber) {
largestNumber = candidate;
}
}
return largestNumber + "";
};
var multiply = function(matches) {
return (parseInt(matches[1]) * parseInt(matches[2])) + "";
};
var drWho = function(matches) {
return "Sean Connery";
};
var plus = function(matches) {
return (parseInt(matches[1]) + parseInt(matches[2])) + "";
};
var minus = function(matches) {
return (parseInt(matches[1]) - parseInt(matches[2])) + "";
};
var banana = function(matches) {
return "yellow";
};
var isSquare = function(num) {
return (Math.sqrt(num) % 1 === 0);
};
var isCube = function(num) {
return (Math.pow(num, 1/3) % 1 === 0);
};
var squareCube = function(matches) {
var numbers = matches[1].split(",");
for (var i = 0; i < numbers.length; i++) {
var num1 = parseInt(numbers[i].trim());
if(isSquare(num1) && isCube(num1)){
return num1 + "";
}
};
return "";
};
var pow = function(matches) {
var num1 = parseInt(matches[1]);
var num2 = parseInt(matches[2]);
return Math.pow(num1, num2) + "";
};
var fib = function(matches) {
var index = parseInt(matches[1]);
var x0 = 0;
var x1 = 1;
var x2;
if(index < 1) return (index) + "";
for(var i = 2; i <= index; i++) {
x2 = x0 + x1;
x0 = x1;
x1 = x2;
}
return x2 + "";
};
var twitter = function(matches) {
return "olemartin";
};
var plusMult = function(matches) {
var num1 = parseInt(matches[1]);
var num2 = parseInt(matches[2]);
var num3 = parseInt(matches[3]);
return (num1 + num2 * num3) + "";
};
var plusPlus = function(matches) {
var num1 = parseInt(matches[1]);
var num2 = parseInt(matches[2]);
var num3 = parseInt(matches[3]);
return (num1 + num2) + num3 + "";
};
function isPrime(n) {
if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false;
var m=Math.sqrt(n);
for (var i=2;i<=m;i++) if (n%i==0) return false;
return true;
};
var primeCheck = function(matches) {
var numbers = matches[1].split(",");
var primes = "";
for (var i = 0; i < numbers.length; i++) {
var num = parseInt(numbers[i].trim());
if(isPrime(num)) {
return "";
}
}
}
var patterns = [
{ pattern: /^.*what is your name$/, ans: myName },
{ pattern: /^.*my name is (.*), what is my name$/, ans: yourName },
{ pattern: /which of the following numbers is the largest: (.*)/, ans: compareNumbers2},
{ pattern: /what is (.*) multiplied by (\d+)/, ans: multiply},
{ pattern: /who played James Bond in the film Dr No/, ans: drWho },
{ pattern: /what is (\d*) plus (\d+)/, ans: plus},
{ pattern: /what is (\d*) minus (\d*)/, ans: minus},
{ pattern: /what colour is a banana/, ans: banana},
{ pattern: /which of the following numbers is both a square and a cube: (\*)/, ans: squareCube},
{ pattern: /what is (\d+) to the power of (\d+)/, ans: pow },
{ pattern: /what is the (\d+)th number in the Fibonacci sequence/, ans: fib},
{ pattern: /what is the twitter id of the organizer of this dojo/, ans: twitter },
{ pattern: /what is (\d+) plus (\d+) multiplied by (\d+)/, ans: plusMult},
{ pattern: /what is (\d+) plus (\d+) plus (\d+)/, ans: plusPlus},
{ pattern: /which of the following numbers are primes: (.*)/, ans: primeCheck }
];
var answer = function(question, req, res) {
for (var i = 0; i < patterns.length; i++) {
var regex = patterns[i].pattern;
if(regex.test(question)) {
return patterns[i].ans.call(this, question.match(regex));
}
};
return "";
};
var app = express.createServer();
app.use(express.cookieParser());
app.use(express.session({
"secret": "tomaspwnz"
}));
app.get("/", function(req, res) {
var q = req.param("q");
var a = answer(q, req, res);
console.log("Q: \"" + q + "\" A:\"" + a + "\"");
res.end(a);
});
app.listen(1337, "0.0.0.0");
console.log("Server running on http://0.0.0.0:1337/");
/* http://10.0.20.138:3000/players/7ad6c580*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment