On my miniquest to improve my Javascript, I'm building some quick games that are designed to not take long to code. I have a timed add/sub like so:
function flash() {
size = 3
rand1 = Math.floor((Math.random() * 201) - 100);
rand2 = Math.floor((Math.random() * 201) - 100);
randMax = Math.max(rand1,rand2)
randMin = Math.min(rand1,rand2)
if (randMax - randMin > 0 ){
if (i < size) {
timer = setTimeout(function() {
display.value = randMax;
answer += randMax;
setTimeout(function() {
display.value = '';
}, 1000);
flash();
}, 5000);
i++;
}} else {
clearTimeout(timer);
}
}
To prevent a negative answer for both the final result AND any on-the-fly result, is the only way to populate the answer in an array beforehand, and compare max/min as we iterate through the array? (eg. [1,-3,4,1,8] would fail 'always >= 0' parameters, while [1,-1,3,-2,4] would pass. If so/if not, how would you approach it?
Aucun commentaire:
Enregistrer un commentaire