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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container ">
  <h1>freeCodeCamp JavaScript Calculator</h1>
  <div class="main">
    <div class="col-md-12">
      <div class="result">
        <p>0</p>
      </div>
    </div>
    <div class="keyboard">
     <!-- Line 1 -->
      <button type="button" action="AC" class="btn btn-danger">AC</button>
      <button type="button" action="CE" class="btn btn-danger">CE</button>
      <button type="button" action="/" class="btn btn-secondary">/</button>
         <button type="button" action="*" class="btn btn-secondary">x</button>
      
      <!-- Line 2 -->
      <button type="button" action="7" class="btn btn-secondary">7</button>
      <button type="button" action="8" class="btn btn-secondary">8</button>
      <button type="button" action="9" class="btn btn-secondary">9</button>
          <button type="button" action="-" class="btn btn-secondary">-</button>
   
      <!-- Line 3 -->
      <button type="button" action="4" class="btn btn-secondary">4</button>
      <button type="button" action="5" class="btn btn-secondary">5</button>
      <button type="button" action="6" class="btn btn-secondary">6</button>
        <button type="button" action="+" class="btn btn-secondary">+</button>
  
      <!-- Line 4 -->
      <button type="button" action="1" class="btn btn-secondary">1</button>
      <button type="button" action="2" class="btn btn-secondary">2</button>
      <button type="button" action="3" class="btn btn-secondary">3</button>
    
      <!-- Line 5 -->
   
      <button type="button" action="." class="btn btn-secondary">.</button>
      <div class="bottom">
          <button type="button" action="0" class="zero btn btn-secondary">0</button>
      <button type="button" action="=" class="equal btn btn-secondary">=</button>
      </div>
    </div>
  </div>
  
</div>
            
          
!
            
              h1{
  padding:25px 0;
  text-align:center;
  font-size:32px;
}
.main{
  margin:auto;
  width:300px;
  border-radius:2px;
      box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  border: 2px solid rgba(0, 0, 0, 0.25);
}
.keyboard{
  width:90%;
    margin:auto;
    background-color:white;
  padding:15px 0;
}
.btn.btn-danger{
  width:45%;
  background-color:#d9534f;
}
.btn.btn-danger:hover{
  background-color:#d9534f; 
}
.btn{
  border:none;
  margin:8px 11.5px;
  padding:7px 8px;
  min-width:40px;
  max-width:40px;
  background-color:#326273;
  color:white;
  font-weight:bold;
}
.btn:hover{
  cursor:pointer;
  background-color:#326273;
  color:white;
}
.col-md-12{
  min-height:65px;
  max-height:65px;
    background-color: darkgray;
  border-bottom: 2px solid darkslategray;
}
.bottom{
  padding:0 10px;
  margin-top:8px;
}
.equal{
  max-width:none;
  width:45%;
  display:inline-block;
  margin: 0 0 0 8%;
}
.zero{
  max-width:none;
  width:45%;
  display:inline-block;
  margin:0;
}
p{
  font-size:35px;
  text-align:right;
  line-height:65px;
}
            
          
!
            
              //Array to store the values inserted by the user
var entrys = [];

//String to Calculate && Return the values inputed by the user into the screen
var completestring;

//The operators we need to control to be sure that we dont have operators followed by others
var operators = ["/","+","-","*"];
var dot = ".";
$("button").click(function() {
  
  //checks if the value clicked is ac if it is return nothing on screen
  if(this.getAttribute("action") === "AC"){
    entrys = ["0"]
    updateScreen();
     
  }
  
 //checks if the value clicked is ce if it is then will remove by every click the last value inserted in the array returns nothing when array is equal to empty set's the first number to 0 if the array doesnt contain nothing
  else if(this.getAttribute("action") === "CE"){
    if(entrys.length > 1){
    entrys.pop();
    updateScreen();
    }else{
      entrys = ["0"]
    updateScreen();
    }
  }
  
  //check if the value is equal to = if it is it will call the total of calculation asked to be made
  else if(this.getAttribute("action")==="="){
    totalValue();
  }
 else{
    getValue(this.getAttribute("action"));
  }
});

//updates the value displayed on the screen based on the entrys array content
function updateScreen (){
  completestring = entrys.join("");
  $(".result p").html(completestring);
}

//joins all the values into one string then calls eval method to calculate everything inside the string
function totalValue(){
  completestring = entrys.join("");
  $(".result p").html(eval(completestring));
}

function getValue(value){
  //Checks if the value inputed exist on the operators list and the last value on the array is aswell on the operators that means we can't use one operator followed by another || Checks if the last value on the array exist in the operators list and if the value inputed by the user is equal to a "." if it does the program can't proceed || Checks if the value inputed its a operator and if the last value in the array is a dot || 
  if(entrys.length === 14){
     console.log("You can't write more than 14 number")
     }
  else if((operators.includes(value)
    && operators.includes(entrys[entrys.length-1])) || (operators.includes(entrys[entrys.length-1]) && value === dot[0] )||((entrys[entrys.length-1]) === "." && operators.includes(value) )){
     console.log("You can't use multiple operators together");
    
    //check if the array length is equal to one and if it includes the 0 as the only value it contains and if the value inputed is not a dot
  }else if(entrys.length === 1 && entrys.includes("0") && value !== dot[0]){
      entrys.pop();
      entrys.push(value);  
    
    //Checks if the array doesnt contain any value and if the value inputed is not a operator
  }else if(entrys.length===0 && operators.includes(value)===false){
    entrys.push(value);
    
    //Checks if the last value in the array is a dot and if the value we just inputed is aswell a dot
  }else if(entrys[entrys.length-1] === dot && value === dot){
    console.log("You can't use two times");
    
    //Checks if the array contains any dot and if the value inputed its a dot
  }else if(entrys.indexOf(dot) !== -1 && value === dot){
    console.log("Use only one decimal per calculation");
    
    //Checks if the last value in the array its not a operator and if the entrys.length is not 0
  }else if(operators.includes(entrys[entrys.length-1])===false && entrys.length !== 0){
    entrys.push(value);
    
    //checks if the value inputed its a number
  }else if (!Number.isNaN(parseInt(value))){
    entrys.push(value);
  }
  updateScreen(); 
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console