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.

            
              <html>

<head>
  <meta charset="UTF-8">
  <title>Calculator</title>
  <link rel="stylesheet" href="css/style.css">
  <link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet">

  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css'>
  <audio id="buttonSound" src="http://www.soundjay.com/button/beep-07.wav" autostart="false"></audio>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>
    <div class="mo-page">
    <div class="mo-title text-center">
      <p>Calculator</p>

      <div class="mo-case">

        <table class="mo-table">
          <tr>
            <td colspan="4" align="left">
            <span class="mo-display"></span>
            </td>
          </tr>
          <tr>
            <td colspan="4" align="left">
            <span class="mo-progress"></span>
            </td>
          </tr>
          <tr>
            <td>
              <span id="7" class="mo-butt mo-7">7</span>
            </td>
            <td>
              <span id="8" class="mo-butt mo-8">8</span>

            </td>
            <td>
              <span id="9" class="mo-butt mo-9">9</span>
            </td>
            <td>
              <span id="PL" class="mo-butt mo-plus">+</span>
            </td>
          </tr>
          <tr>
            <td>
              <span id="4" class="mo-butt mo-4">4</span>
            </td>
            <td>
              <span id="5" class="mo-butt mo-5">5</span>
            </td>
            <td>
              <span id="6" class="mo-butt mo-6">6</span>
            </td>
            <td>
              <span id="MI" class="mo-butt mo-minus">-</span>
            </td>
          </tr>
          <tr>
            <td>

              <span id="1" class="mo-butt mo-1">1</span>
            </td>
            <td>
              <span id="2" class="mo-butt mo-2">2</span>
            </td>
            <td>
              <span id="3" class="mo-butt mo-3">3</span>
            </td>
            <td>
              <span id="MU" class="mo-butt mo-multiply">x</span>
            </td>
          </tr>
          <tr>
            <td>
              <span id="0" class="mo-butt mo-0">0</span>
            </td>
            <td>
              <span id="DP" class="mo-butt mo-decplace">.</span>
            </td>
            <td>
              <span id="EQ" class="mo-butt mo-equals">=</span>
            </td>
            <td>
              <span id="DI" class="mo-butt mo-divide">/</span>
            </td>
          </tr>
          <tr>
            <td colspan="4">
            <span id="AC" class="mo-butt-wide mo-butt mo-ac">AC</span>
            </td>
          </tr>
        </table>

      </div>
      <div class="mo-credit text-center">
        <br>

          by <a href="https://codepen.io/pcblues">Mark Daniel Osborne</a>
        
      </div>

    </div>

  </div>
  <script src="js/index.js"></script>

</body>

</html>
            
          
!
            
              
html, body
{
  height: auto;
}

.mo-case
{
  border-color:#3A4C7D;border-width: 0px 0px 2px 0px;border-style: solid; -webkit-border-radius: 3px; -moz-border-radius: 3px;border-radius: 3px;font-size:12px; padding: 10px 10px 10px 10px; text-decoration:none; display:inline-block;text-shadow: -1px -1px 0 rgba(0,0,0,0.3);font-weight:bold; color: #FFFFFF;

  background-color: #A5B8DA; background-image: -webkit-gradient(linear, left top, left bottom, from(#A5B8DA), to(#7089B3));
  background-image: -webkit-linear-gradient(top, #A5B8DA, #7089B3);
  background-image: -moz-linear-gradient(top, #A5B8DA, #7089B3);
  background-image: -ms-linear-gradient(top, #A5B8DA, #7089B3);
  background-image: -o-linear-gradient(top, #A5B8DA, #7089B3);
  background-image: linear-gradient(to bottom, #A5B8DA, #7089B3);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#A5B8DA, endColorstr=#7089B3);
}

.mo-table
{
  margin:0px auto;
}

.mo-display{
  font-size: 20px;
  background-color: #E0E6F1; 
  border-color:#3A4C7D;border-width: 1px 1px 1px 1px;
  border-style: solid; 
  -webkit-border-radius: 3px; 
  -moz-border-radius: 3px;
  border-radius: 3px;
  padding: 5px 5px 5px 5px; text-decoration:none; display:inline-block;
  font-weight:bold; 
  color: #4B4A98;
  min-width:100%;
}

.mo-progress {
  font-size: 10px;
  background-color: #BECFEC; 
  border-color:#3A4C7D;
  border-width: 0px 0px 0px 0px;
  border-style: solid; 
  -webkit-border-radius: 3px; 
  -moz-border-radius: 3px;
  border-radius: 3px;
  padding: 2px 2px 2px 2px; text-decoration:none; display:inline-block;
  font-weight:bold; 
  color: #000000;
  min-width:100%;
}


.mo-page
{
  font-family: 'Orbitron', sans-serif;background-color: #000000;
}

.mo-butt{
border-color:#3A4C7D;border-width: 0px 0px 2px 0px;border-style: solid; -webkit-border-radius: 3px; -moz-border-radius: 3px;border-radius: 3px;font-size:12px; padding: 10px 10px 10px 10px; text-decoration:none; display:inline-block;text-shadow: -1px -1px 0 rgba(0,0,0,0.3);font-weight:bold; color: #FFFFFF;
 background-color: #A5B8DA; background-image: -webkit-gradient(linear, left top, left bottom, from(#A5B8DA), to(#7089B3));
 background-image: -webkit-linear-gradient(top, #A5B8DA, #7089B3);
 background-image: -moz-linear-gradient(top, #A5B8DA, #7089B3);
 background-image: -ms-linear-gradient(top, #A5B8DA, #7089B3);
 background-image: -o-linear-gradient(top, #A5B8DA, #7089B3);
 background-image: linear-gradient(to bottom, #A5B8DA, #7089B3);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#A5B8DA, endColorstr=#7089B3);
 
 min-height: 20px;
 min-width: 40px;
}



.mo-butt-wide
{
  min-width:100%;
}

.mo-butt:hover{
 border-color:#3A4C7D;border-width: 0px 0px 2px 0px;border-style: solid;
 background-color: #819BCB; background-image: -webkit-gradient(linear, left top, left bottom, from(#819BCB), to(#536F9D));
 background-image: -webkit-linear-gradient(top, #819BCB, #536F9D);
 background-image: -moz-linear-gradient(top, #819BCB, #536F9D);
 background-image: -ms-linear-gradient(top, #819BCB, #536F9D);
 background-image: -o-linear-gradient(top, #819BCB, #536F9D);
 background-image: linear-gradient(to bottom, #819BCB, #536F9D);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#819BCB, endColorstr=#536F9D);
}

.mo-box
{
    margin-top: 10px;
    margin-bottom: 10px;
    box-shadow: 0 0 10px black;
    padding:0 15px 0 15px;

}
  
.mo-title 
{
  
  font-size: 20px;
  padding:10px 15px 10px 15px;
  color: #A5B8DA

}

.mo-logo
{
  max-height: 50px;
}

.mo-icon
{
  background-size: 100px;
  background-repeat: no-repeat;
    background-position: center;
    min-height:100px

}


.mo-credit 
  {
    font-size: 10px;
    color: #A5B8DA

  }

.mo-credit a
  {
    font-size: 10px;
    color: #A5c8fA
  }
            
          
!
            
              /* Calculator


TODO:
Process command chain
Finish use cases

LATER:
Make default progress text blank without wrecking height of box

DONE:
Error: AddEventListener is not a function
Design look/font
- css buttons
- google font
Stretch displays across box
- use table?
Buttons same size
Rows/cols
Display total
Display chained commands

TO RUN:
http-server in fcc directory
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
Run app in code
Refresh in browser

OBJECTIVE: 
Build a CodePen.io app that is functionally similar to this: https://codepen.io/FreeCodeCamp/full/rLJZrA/.

Fulfill the below user stories. Use whichever libraries or APIs you need. Give it your own personal style.

DONE User Story: I can add, subtract, multiply and divide two numbers.

DONE User Story: I can clear the input field with a clear button.

User Story: I can keep chaining mathematical operations together until I hit the equal button, and the calculator will tell me the correct output.

Remember to use Read-Search-Ask if you get stuck.

When you are finished, click the "I've completed this challenge" button and include a link to your CodePen.

You can get feedback on your project by sharing it with your friends on Facebook.


*/  
var defaultCommand="input"
var defaultDisplay = "0"
var defaultError = "error"
var commandString = defaultCommand
var displayString = defaultDisplay
var clearOutput =false

function processResult() {
  var result = 0;
  var comms=commandString.split(" ")
  var noInput = true  
  var nextEl= ""
  var el = ""
  var lastEl = ""
  for (var c=0;c<comms.length-1;c++) {
    lastEl=0
    el=(comms[c])
    if (el!="") {
      nextEl=(comms[c+1])
      if (el=="+") {
        result+=parseFloat(nextEl)
      } else if (el=="-") {
        result-=parseFloat(nextEl)
        
      } else if (el=="x") {
        result*=parseFloat(nextEl)
        
      } else if (el=="/") {
        result/=parseFloat(nextEl)
        
      } else if (el=="="){
        if (noInput==true) {
          result = parseFloat(lastEl)
        }
      } else {
        // number
        if (noInput==true) {
          result = parseFloat(el)
        }
      }
      noInput = false
      } 
    }

  
  displayString = result
  commandString+=" "+result
  clearOutput = true
}

function playButtonPress() {
      var sound = document.getElementById("buttonSound");
      sound.play();
  }

function updateScreen() {
  $(".mo-display").html(displayString) 

  $(".mo-progress").html(commandString) 
}
  
function addCommand(pCom) {
  if (clearOutput==true) {
    commandString = ""
    clearOutput =false
  }
  if (commandString==defaultCommand) {
    commandString = ""
  }
  commandString+=pCom
}
function buttClick() {
  playButtonPress();
  if (this.id=="AC") {
    displayString = defaultDisplay
    commandString = defaultCommand
  }  else 
  if (this.id=="PL") {
    addCommand(" + ")
  }  else
  if (this.id=="MI") {
    addCommand(" - ")   
  }  else
  if (this.id=="MU") {
    addCommand(" x ")
    
  }  else
  if (this.id=="DI") {
    addCommand(" / ")
    
  }  else
  if (this.id=="DP") {
    addCommand(".")
    
  }  else
  if (this.id=="EQ") {
    addCommand(" = ")
    processResult();
  }  else {
    // a number
    addCommand(this.id)
    
  }
  updateScreen()
}


$(document).ready(function() {
  updateScreen()
  addLog("Document Ready")
  var butts = document.getElementsByClassName("mo-butt")
  for (a of butts) {
    a.addEventListener("click",buttClick);// addeventlistener    
  }

})

var addLog = function(msg) {
  //$(".mo-log").append(msg+"<BR>")
  console.log(msg)
}


            
          
!
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