Last active
August 29, 2015 14:00
-
-
Save lrdiv/4d40a926478438f07645 to your computer and use it in GitHub Desktop.
A Twitter friend mentioned that her 3rd grade daughter had some tough math homework. Tried to solve it with JS.
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
var firstClue = function(min, max, digitSum) { | |
for(var i = min; i < max; i++) { | |
var digitArray = i.toString().split(''); | |
var digitTotal = 0; | |
digitArray.forEach(function(digit) { | |
digitTotal += parseInt(digit); | |
}); | |
if ( digitTotal < digitSum ) { | |
return i; | |
} | |
} | |
} | |
var secondClue = function(min, max, endsIn, fifth) { | |
for(var i = min; i < max; i++) { | |
if ( ( ( i * 2 ).toString().split('')[1] == endsIn.toString() ) && ( i / 5 === fifth ) ) { | |
return i; | |
} | |
} | |
} | |
var thirdClue = function(min, max, digitSum, odd) { | |
for(var i = min; i < max; i++) { | |
var halfsies = i / 2 | |
var digitTotal = 0; | |
var digitArray = i.toString().split(''); | |
digitArray.forEach(function(digit) { | |
digitTotal += parseInt(digit); | |
}); | |
if ( ( digitTotal === digitSum ) && ( halfsies % 2 === 1 ) ) { | |
return i; | |
} | |
} | |
} | |
var fourthClue = function(min, max, bySix, byFour) { | |
for(var i = min; i < max; i++) { | |
var doubled = i * 2; | |
var bySixResult = ( parseInt( ( i * 6 ).toString().split('')[1] ) === bySix ) | |
var byFourResult = ( parseInt( ( i * 4 ).toString().split('')[1] ) === byFour ) | |
if ( ( doubled > 20 ) && ( doubled < 40 ) && ( bySixResult ) && ( byFourResult ) ) { | |
return i; | |
} | |
} | |
} | |
var fifthClue = function() { | |
return "This has 2 answers I don't even... [12, 24]"; | |
} | |
$(document).ready(function() { | |
$('button').on('click', function() { | |
var questionNumber = $(this).attr('class').split('question-')[1][0]; | |
var questionAnswer = $('.question-' + questionNumber + '-answer').html(); | |
if ( questionAnswer.length == 0 ) { | |
switch(questionNumber) { | |
case "1": | |
appended = firstClue(31, 40, 5); | |
break; | |
case "2": | |
appended = secondClue(16, 40, 0, 5); | |
break; | |
case "3": | |
appended = thirdClue(0, 100, 4, true); | |
break; | |
case "4": | |
appended = fourthClue(1, 100, 8, 2); | |
break; | |
case "5": | |
appended = fifthClue(); | |
break; | |
} | |
$('.question-' + questionNumber + '-answer').html(appended); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment