Skip to content

Instantly share code, notes, and snippets.

@v2keener
Last active December 17, 2015 23:39
Show Gist options
  • Select an option

  • Save v2keener/5690878 to your computer and use it in GitHub Desktop.

Select an option

Save v2keener/5690878 to your computer and use it in GitHub Desktop.
This is embeddable code for doing multiplication calculations
<div class="This-is-just-an-enclosure">
<div id='mathOut'> </div>
<div><input type='text' id='testasdf' /><input type='button' id='testbutton' value='Tell Me The Answer' /></div>
<div id="result"> </div>
<script type="text/javascript">
(function(){
var $ = jQuery;
var upperLimit = 13, lowerLimit = 1;
var math$ = $('#mathOut');
var result$ = $('#result');
var button$ = $('#testbutton');
var text$ = $('#testasdf');
var getRandom12 = function(){
return Math.floor((Math.random()*100)%(upperLimit-lowerLimit+1))+lowerLimit;
};
var resetAll = function(){
text$.val('');
result$.html('');
getNext();
};
var lastGuess = {x:0,y:0};
var getNext = function(){
var x = getRandom12(), y = getRandom12();
// No two duplicate queries in a row (does not account for same different combinations, same numbers
while(x === lastGuess.x && y === lastGuess.y){
x = getRandom12(), y = getRandom12();
};
var result = x * y;
(function(x,y,result){
lastGuess.x = x, lastGuess.y = y;
var guessCount = 1;
var plurales = '';
var plural = '';
//var start = new Date();
math$.html("(Type the answer below and hit ENTER)<br />What is " + x + " x " + y + "?");
button$.unbind('click').click(function(){
$("<div>Admitted defeat at " + guessCount + " attempt"
+ plural + ". "+ result + " = " + x + " x " + y + "</div>").insertAfter(result$);
resetAll();
});
text$.unbind('keypress').keypress(function(e){
if(e.keyCode === 13){
if(result.toString() == text$.val()){
// 'start' can be used here to tell how long it took to enter the correct answer
$('<div>' + result + ' is the correct answer at ' + guessCount
+ ' guess' + plurales + '</div>').insertAfter(result$);
resetAll();
}
else{
result$.html('Incorrect at ' + guessCount++ + ' attempt' + plural + '...');
plural = 's'
plurales = 'es';
}
}
});
})(x,y,result);
};
// Let the guessing begin!
getNext();
})();
</script>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment