Skip to content

Instantly share code, notes, and snippets.

@rubennorte
Last active August 29, 2015 13:56
Show Gist options
  • Save rubennorte/8960827 to your computer and use it in GitHub Desktop.
Save rubennorte/8960827 to your computer and use it in GitHub Desktop.
/*
* 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