Menu

Fonction calculatrice [Résolu]

Messages postés
2
Date d'inscription
vendredi 2 novembre 2018
Dernière intervention
12 novembre 2018
-
bonjour, il y'a un message calculer is not defined

at HTMLButtonElement.onclick (calcul.html:99)

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
 
    <title>calculette</title>
 <style>
  #ecran {
    background : black;
    color :#7f7;
    text-align : right;
  width : 100%;
  box-sizing: border-box;
  
  padding : 6px;
  font-size : 1.2em;
   }
   .btn {
    width : 40px;
    height : 40px;
   }
   .gbtn{
    width : 100%;
    
   }
   
   </style>
   
   
   <script>
    var operande1 =  '0';
    var operator = '+';
    var test = false;
     function calculer(g,v){
    //console.log (g,v);
    var ecran = document.querySelector('#ecran');
    switch (g){
     case 'ch':
     if (test) {
     ecran.value = "";
     test = false;
     }
      if(ecran.value == '0' ) ecran.value='';
      ecran.value + = v;
   
     break;
     case 'dec':
     if (ecran.value.indexOf('.')== -1)
     ecran.value + = '.';
     break;
     case 'op':
     calculer('eg');
     operande1 = ecran.value;
     operator = v;
     test = true;
     
     break;
     case 'eg':
     switch(operator){
      case '+':
    ecran.value+ = ecran.value*1 + parseFloat(operande1);
   break;
   case '-':
   ecran.value  = operande1-ecran.value;
   break;
   case '/':
   ecran.value  = operande1/ecran.value;
   break;
   case 'x':
   ecran.value  = operande1*ecran.value;
   break;
   }
     break;
     case 'sp':
     
     break;
    }
     
    </script>
</head>

<body>
  <table border="1">
 <tr>
    <td colspan="5">
  <input type ="text" id="ecran" value ="0" size= "10" ></td>
  </tr>
  <tr>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',7)">7</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',8)">8</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',9)" >9</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('sp','c')">C</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('sp','ce')">CE</button></td>
  </tr>
  <tr>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',4)">4</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',5)">5</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',6)">6</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('op','+')">+</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('op','-')">-</button></td>
  </tr>
  <tr>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',1)">1</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',2)">2</button></td>
  <td> <button class ="btn" onClick = "JavaScript :calculer('ch',3)">3</button></td>
  <td> <button class ="btn" onClick = "JavaScript:calculer('op','x')">x</button></td>
  <td> <button class ="btn" onClick = "JavaScript:calculer('op','/')">/</button></td>
  </tr>
  <tr>
  <td colspan= "2" > <button class ="btn gbtn" onClick = "JavaScript:calculer('ch',0)">0</button></td>
  <td> <button class ="btn" onClick= "JavaScript:calculer('dec')" >,</button></td>
  <td colspan= "2" > <button class ="btn gbtn" onClick = "JavaScript:calculer('eg','')">=</button></td>
  
  </tr>
  </table>
  
</body>

</html>


EDIT : Ajout des balises de code
Afficher la suite 

Votre réponse

1 réponse

0
Merci
Salut,

Remplace les "+ =" par "+=".
Il manque une accolade à la fin de la fonction.
nourkali
Messages postés
2
Date d'inscription
vendredi 2 novembre 2018
Dernière intervention
12 novembre 2018
-
merci beaucoup
Commenter la réponse de Zero