Last active
August 29, 2015 13:56
-
-
Save rubennorte/8960827 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
/* | |
* Script to cheat SpeedSums.com test | |
*/ | |
function getResult(expression){ | |
var regex = /\s*(\d+)\s*([^\s])\s*(\d+)/; | |
var matches = regex.exec(expression); | |
if (matches){ | |
var x = parseInt(matches[1], 10), | |
op = matches[2], | |
y = parseInt(matches[3], 10); | |
switch(op){ | |
case '+': return x+y; | |
case '-': return x-y; | |
case 'x': return x*y; | |
case '÷': return x/y; | |
default: console.log('Operation ' + op + ' not supported'); | |
} | |
} | |
} | |
function solve(){ | |
var answer = $('#answer'); | |
if (answer.size() === 0){ | |
return; | |
} | |
var current = answer.val(); | |
if (!current){ | |
var text = $('#question').text(); | |
var result = getResult(text); | |
if (typeof result !== 'number'){ | |
alert('Expression ' + text + ' cannot be solved'); | |
return; | |
} | |
answer.val(result); | |
answer.trigger('keyup'); | |
} | |
setTimeout(solve, 100); | |
} | |
solve(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment