Skip to content

Instantly share code, notes, and snippets.

@plusjade
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plusjade/e4512842ffbf4dcae8d6 to your computer and use it in GitHub Desktop.
Save plusjade/e4512842ffbf4dcae8d6 to your computer and use it in GitHub Desktop.
var questions = [
"Two + Three",
"Five + Four",
"Nine - Two",
"Five - Three",
"Six + Three"
];
var answers = [
5, 9, 7, 2, 9
];
var numbers = [
"Zero",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine"
];
// Solution if you're bored
var randIndexA = Math.floor(Math.random() * numbers.length); // Number #1
var randIndexB = Math.floor(Math.random() * numbers.length); // Number #2
var a = Math.round(Math.random() * 1); // 0 for minus, 1 for plus
var b;
if ( a ) { b = " + "; } else { b = " - "; }
console.log( "First number is: " + randIndexA + " (" + numbers[randIndexA] + ")" );
console.log( "First number is: " + randIndexB + " (" + numbers[randIndexB] + ")" );
console.log( "Math symbol to use:" + b );
$("#question").html( numbers[randIndexA] + b + numbers[randIndexB] );
$("#checkAnswer").click(function() {
console.log("Check Answer was clicked!");
var userIn = parseInt( $( "#userinput" ).val() );
var message = ( calculate(randIndexA, randIndexB, a) === userIn )
? "You're a human"
: "You're either a robot or bad at math"
;
$( "#result" ).html(message);
});
// Add or subtract two numbers based on a custom operator.
function calculate(a, b, sign) {
if (sign === 1) { // I just like to be ultra clear here.
return (a + b);
} else {
return (a - b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment