cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <body>
  <div class="container">
    <div class="calculette">
      <h1>Simple Math Calcul</h1>

      <!-- SCREEN -->
      <div id="screen">
        <div id="result">
          0
        </div>
      </div>


      <!-- BUTTONS -->
      <div class="buttons">
        <button class="red" value="ac">AC</button>
        <button class="red" value="ce">CE</button>
        <button value="/">&divide;</button>
        <button value="x">x</button>

        <button value="7">7</button>
        <button value="8">8</button>
        <button value="9">9</button>
        <button value="-">-</button>

        <button value="4">4</button>
        <button value="5">5</button>
        <button value="6">6</button>
        <button value="+">+</button>


        <button value="1">1</button>
        <button value="2">2</button>
        <button value="3">3</button>
        <button class="invisible">N</button>

        <button class="equal" value="=">=</button>
        <button class="zero" value="0">0</button>
        <button value=".">.</button>
        
      </div>
    </div>
    <div class="footer">Inspired by FCC, coded by Alexandre Robin</div>
</body>
            
          
!
            
              body {
  background-color:#004D40;
}
.calculette{
  height: 410px;
  width: 300px;
  margin-top: 10%;
  margin-left: auto;
  margin-right: auto;
  background-color: #E0F2F1;
  border: 2px solid #908b85;
  border-radius: 20px;
  box-shadow: 0px 3px 5px 0px #222121, inset -4px 0px 30px 0px #004D40
}
h1{
  text-align:center;
  font-size:1.7em;
  margin:4%
  
}

#screen{
  margin:auto;
  height:40px;
  width:80%;
  border:2px solid #908b85;
  border-radius:5px;
  background-color: #757575;
  text-align:right;
  font-size:1.8em; 
  padding-right:5px;
  padding-bottom:14%;
  overflow:hidden;
}

.buttons{
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  position: absolute;
  display: inline-block;
  width: 280px;
  height: auto;
  margin-top: 15px;
  margin-left: 15px;
}
button{
  border-radius: 8px;
  border: 1px solid black;
  background-color: #009688;
  margin: 0 4px 10px 8px;
  height: 40px;
  width: 50px;
  box-shadow: 0px 3px 0px 0px #222121, inset -1px -3px 20px 1px #515151
}
button:hover{
  cursor:pointer;
  opacity:0.9;
}

.red{
  background-color:#F44336;
  box-shadow: 0px 3px 0px 0px #222121, inset -1px -3px 10px 1px #515151
}
.red:hover{
}
.zero{
  width: 117px;
}
.equal{
  position: absolute;
  margin-left: -58px;
  margin-top: 0px;
  height: 90px;
  
}

.footer{
  text-align:center;
  padding-top:30%;
  color:C8E6C9;
}

            
          
!
            
                /* User Story
   I can add, subtract, multiply and divide two numbers.
   I can clear the input field with a clear button.
   I can keep chaining mathematical operations together until I hit the equal button, and the calculator will tell me the correct output.

   */

 /* TODO :
 - Set a max digits limit --> le résultat est affiché avec un maximum de 13 caractères (lignes 118-120)
 - Round val */

 $(function() {
   var buttonPushed = "";
   var entry = "";
   var answer = "";
   var memory = "";
   var operators = [];
   var operation = [];
   var initialized = true;
   var result = [];
   var x = 0;
   var y = 0;
   var num1 = 0;
   var num2 = 0;
   var count = 0;
   var multiOrDiv = false;
   var operator = [];

   //https://jsfiddle.net/KJ6Tv/1/

    
     function additionOrSubstraction() {
       for (var i=0;i<result.length;i++) {  //ta boucle '(var i in result)' ne fonctionne pas avec la suite telle que je l'ai modifiée 
         if ((result[i] === "+")|| (result[i] === "-")) {
           num1 = Number(result[i-1]);  //parseFloat (que j'ai laissé tel quel ailleurs) est OK mais Number() suffit
           num2 = parseFloat(result[i+1]);
           var res;
           if (result[i] === "+") res = num1+num2;
           else res = num1 - num2;
           result.splice(0,3,res);  //élimine les 3 premiers éléments et place 'res' en position 0 dans 'result'
           console.log(result)
         }
       }
     }  //fin de la fonction additionOrSubstraction() 

     function multiplicationOrDivision() {
       if (result.join('').search("/") + result.join('').search("x") >-1) {
         multiOrDiv = true;
       } else {
         multiOrDiv = false
         additionOrSubstraction();
       }
       for (var i=0;i<result.length;i++) {  //ta boucle '(var i in result)' ne fonctionne pas avec la suite telle que je l'ai modifiée
         if (result[i] === "/" || result[i] === "x" && multiOrDiv === true) {
           num1 = parseFloat(result[i-1]); 
           num2 = parseFloat(result[i+1]);
           var res;          
           if (result[i] === "x") res = num1 * num2;
           else res = num1 / num2;
           result.splice(0,3,res);  //élimine les 3 premiers éléments et place 'res' en position 0 dans 'result
           console.log(result)
          }
       }
     }  //fin de multiplicationOrDivision()

     function check() {
       if (result.length > 1) {
         multiplicationOrDivision()
         check();
       } else if (result.length > 1 && multiOrDiv === false) {
         additionOrSubstraction();
         check();
       } else if (result.length === 1) {
         answer = result;
       }
     }  //fin de check()

   function mathItUp() {
     // Début de la fonction MathItUp()
     // We need to create en array with numbers and operators separated, it will be stored in "result"
     var temp = entry
     entry = entry.replace(/[0-9]+/g, "#").replace(/[\(|\|\.)]/g, "");
     var numbers = temp.split(/[^0-9\.]+/);
     operators = entry.split("#").filter(function(n) {
       return n
     });
     result = [];
     for (var i in numbers) {
       result.push(numbers[i]);
       if (i < operators.length) result.push(operators[i]);
     }
     console.log(result)
     multiplicationOrDivision();
     check();
   }    //fin de MathItUp()

   $("button").click(function() {
     buttonPushed = $(this).attr("value");

     if (initialized === true) {
       if (buttonPushed === "0" || buttonPushed === "1" || buttonPushed === "2" || buttonPushed === "3" || buttonPushed === "4" || buttonPushed === "5" || buttonPushed === "6" || buttonPushed === "7" || buttonPushed === "8" || buttonPushed === "9" || buttonPushed === ".") {
         
         entry += buttonPushed;
         $("#result").html(entry);
         console.log(entry);
        
       }
     }
     if (buttonPushed === "/" || buttonPushed === "+" || buttonPushed === "-" || buttonPushed === "x") {
       memory = entry;
       operator = buttonPushed;
       operators.push(operator);
       console.log(operators);
       entry += operator;
       $('#result').html(entry);
       initialized = true;
     }
     if (memory != "0" && buttonPushed === "=") {
       mathItUp();
       var s_answer = String(answer[0]).substr(0,13); // 's_answer' est la version String de la première valeur du tableau 'answer'
       $('#result').html(s_answer);
       entry = Number(s_answer); //pour que 'entry' corresponde bien à la valeur numérique de l'affichage
       operators = [];
       initialized = false;
     }
     if (buttonPushed === "ac") {
       entry = "";
       operator = "";
       memory = "";
       answer = "";
       operators = [];
       operation = [];
       initialized = true;
       $('#result').html("0");
     }
     if (buttonPushed === "ce") {
       entry = memory;
       $('#result').html(entry);
     }
   }) //fin de la fonction JQuery '$("button").click'
 });  //fin de la fonction JQuery globale '$(function()'
 
            
          
!
999px
Loading ..................

Console