Entraînement au calcul mental

Module pour le développement de ses capacités de calcul mental, basé sur des opérations aléatoires.

JavaScript

function random(min,max)
{
    var randi = Math.random();
    return Math.floor(randi*(max-min+1)+min);
}
function testCalcul(operators, getQuestion){
  var a = random(2,1000);
  var b = random(2,1000);
  var operator = Object.keys(operators)[random(0,3)];
  var expectedAnswer = operators[operator](a,b);
  
  var question = getQuestion(a,operator,b);
  var answer = prompt(question);
  if (answer == expectedAnswer) {
      alert("Bravo !\n" + question + " = " + expectedAnswer);
      return true;
  }
  alert("Perdu, dommage\n" + question + " = " + expectedAnswer);
  return false;
}
while(true) {
  var operators = {
    '+':function(a,b){return a+b;},
    '-':function(a,b){return a-b;},
    '*':function(a,b){return a*b;},
    '/':function(a,b){return a/b;},
  }
  function getQuestion(a,operator,b){
    return 'Calcul N°'+nbTests+'\n' + a + ' ' + operator + ' ' + b;
  }
  var nbSuccess=0;
  var nbTests=0;
  for(var i=0;i<10;++i){
    if(testCalcul(operators, getQuestion)){
      nbSuccess++;
    }
    nbTests++;
  }
  alert("seulement "+nbSuccess+'/'+nbTests);
}