Created
August 7, 2015 06:59
-
-
Save lukasz-madon/e1192edfc631b4901e83 to your computer and use it in GitHub Desktop.
This file contains 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
"use latest"; | |
let MongoClient = require('mongodb').MongoClient; | |
const questions = ` | |
<html> | |
<head> | |
<title>Euler problems</title> | |
</head> | |
<body> | |
<h1>Euler Project</h1> | |
<p> | |
Hi <%= email %>!<br> | |
Pick one of the following problems and send the solution using query string.<br> | |
https://webtask.it.auth0.com/api/run/wt-lukasz_madon-gmail_com-0/hack?webtask_no_cache=1&question=1&answer=x&email=xxx@xxx.com | |
</p> | |
<h1><%= msg %></h1> | |
<img src="<%= img %>" /> | |
<h2>Project Euler Problem #1</h2> | |
<p> | |
If we list all the natural numbers below 10 that are multiples of 3 or 5, | |
we get 3, 5, 6 and 9. The sum of these multiples is 23. | |
Find the sum of all the multiples of 3 or 5 below 1000. | |
</p> | |
<h2>Project Euler Problem #2</h2> | |
<p> | |
Each new term in the Fibonacci sequence is generated by adding the | |
previous two terms. By starting with 1 and 2, the first 10 terms will be: | |
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... | |
Find the sum of all the even-valued terms in the sequence which do not | |
exceed four million. | |
</p> | |
</body> | |
</html> | |
` | |
const images = [ | |
'http://img-comment-lol.9cache.com/media/1a79ac3c141682746595291692_700wa_0.gif', | |
'http://img-comment-fun.9cache.com/media/aw0npW4/aqzD3EVL_700wa_0.gif' | |
]; | |
// 1st | |
// curr_sum = 0 | |
// for num in xrange(1000): | |
// if num % 5 == 0 and num % 3 == 0: | |
// curr_sum += num | |
// elif num % 5 == 0: | |
// curr_sum += num | |
// elif num % 3 == 0: | |
// curr_sum += num | |
// | |
// print curr_sum | |
// 2nd | |
// s = 0 | |
// f1,f2 = 1,2 | |
// while f1 < 4000000: | |
// if f1 % 2 == 0: | |
// s += f1 | |
// f1, f2 = f2, f1 + f2 | |
// | |
// print s | |
const sendEvent = (url, email, question, success) => { | |
MongoClient.connect(url, (err, db) => { | |
if(err) return console.log(err); | |
db.collection('webtask'). | |
insertOne({ email: email, question: question, success: success }, function (err, result) { | |
if(err) return console.log(err); | |
}); | |
}); | |
}; | |
module.exports = (ctx, req, res) => { | |
const { email, question, answer, MONGO_URL} = ctx.data; | |
let msg = ''; | |
let img = ''; | |
//TODO(lukaszma) add proper validation | |
if(email && question && answer) { | |
if(question === '1' && answer === '233168') { | |
msg = 'Correct answer to the first question!'; | |
img = 'http://i772.photobucket.com/albums/yy8/iRikiPWNurFace/Misc/00-great-success.gif'; | |
sendEvent(MONGO_URL, email, question, true); | |
} else if(question === '2' && answer === '4613732') { | |
msg = 'Correct answer to the second question!'; | |
img = 'http://rs1220.pbsrc.com/albums/dd448/HannahLynnLove/GIF%20Photos/Success.gif~c200'; | |
sendEvent(MONGO_URL, email, question, true); | |
} else { | |
msg = 'Wrong answer or question'; | |
img = images[Math.floor(Math.random() * images.length)]; | |
sendEvent(MONGO_URL, email, question, false); | |
} | |
} | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.end(require('ejs').render(questions, { | |
email: ctx.data.email || '', | |
msg: msg, | |
img: img | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment