Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                #app.calculator
   h1 Vue Calculator
   p A FreeCodeCamp Project
   .display
      p: input(type="text" v-model="result" disabled)#result
      p: input(type="text" v-model="calculation" disabled)#calculation
   p.row
      button AC
      button CE
      button /
      button x
   p.row
      button 7
      button 8
      button 9
      button -
   p.row
      button 4
      button 5
      button 6
      button +
   p.row
      button 1
      button 2
      button 3
      button.tall =
   p.row
      button 0
      button .
      
   
              
            
!

CSS

              
                @import "bourbon";

// Color
$darkBlue: #2c3e50;
$green: #42b983;
$lightGreen: lighten($green,30%);
$grey: #7f8c8d;
$lightGrey: #f8f8f8;

//fonts
@import url(https://fonts.googleapis.com/css?family=Dosis|Source+Sans+Pro|Space+Mono);
$heading-font: 'Dosis', 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif;
$body-font: 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif;
$monospace: 'Space Mono', monospace;

* {
   box-sizing: border-box;
}

html, body {
   width: 100%;
}

button, button:focus {
   outline: none;
   appearance: none;
}


.calculator {
   width: 350px;
   margin: 50px auto 10px auto;
   padding: 25px 10px;
   position: relative;
   font-family: $body-font;
   border: rgba($grey,0.3) solid 2px;
   border-radius: 10px;
   box-shadow: 3px 3px 5px rgba($grey, 0.3);
   background-color: rgba($grey,0.2);
   color: $green;
   text-align: center;
   
   p { margin: 0;}
   
   // Heading
   h1 {
      margin: 0;
      font-family: heading-font;
      font-size: 2.5em;
      &+ p {
         margin: 0px;
         font-size: 1.2em;
         color: $grey;
      }
   }
   .display {
      background-color: $lightGrey;
      margin: 10px auto;
      padding: 10px;
      border: solid 0.5px rgba($grey, 0.4);
      border-radius: 10px;
      box-shadow: inset 0 0 2px rgba($grey, 0.8);
   }
   input[type="text"] {
      width: 100%;
      margin: 0;
      padding: 5px 10px;
      border: none;
      background-color: transparent;
      font-family: $monospace;
      text-align: right;
      &#result {
         font-size: 1.8em;
         font-weight: bolder;
         border-bottom: rgba(0,0,0,0.1) solid 0.5px;
      }
      &#calculation {
         font-size: 1.3em;
      }
      
   }
   
   .row {
      display: flex;
      position: relative;
      width: 100%;
      flex-flow: row nowrap;
      
      button {
         width: 100%;
         height: 60px;
         margin: 5px;
         background-color: $green;
         color: white;
         border: inside $lightGreen solid 1.4px;
         border-radius: 10px;
         box-shadow: 0.5px 0.5px 0px rgba($grey,0.2);
         font-size: 1.3em;
         font-weight: bolder;
         transition: all 0.4s, background-color 0.1s ease-in;
         
         &:hover {
            background-color: rgba($green,0.8);
         }
         
         &:active {
            transform: scale(0.8,0.8);
            border: none;
         }
      }
      
      .tall {
         min-height: 125px;
      }
      
      &:last-child {
         display: flex;
         position: absolute;
         bottom: calc(100% - 580px);
         width: 71%;
      }
   }
}
              
            
!

JS

              
                var calc = new Vue({
	el: "#app",
	data: {
		memory: "",
		current: "",
		result: "0"
	},
	computed: {
		calculation:function(){
         if(this.current.length > 0) {
			   return this.memory + this.current.match(/(\d+[.]\d+)|\d+/g).join("");
         }
         
         return this.memory;
		}
	}
});

$(document).ready(function() {

	function calculate(num1,num2, opr){
		num1 = parseFloat(num1);
		num2 = parseFloat(num2);
		switch(opr){
			case "+":
				return num1 + num2
				break;
			case "-":
				return num1 - num2
				break;
			case "x":
				return num1 * num2
				break;
			case "/":
				if(num2 === 0) { return 0 }
				return num1 / num2
				break;
			default:
				console.log("default");
				return num1 + num2
		}
	}

	$("button").on("click",function(){
		var bVal = $(this).text();
		
		// When AC button has been pressed
		if(bVal == "AC"){
			calc.memory = "";
			calc.current = "";
			calc.result = 0;
		} else

		// When CE button has been pressed
		if(bVal == "CE"){
			calc.current = "";
		} else

		// When number buttons have been pressed
		if(!isNaN(bVal)){
			calc.current += bVal;
		} else

		// When dot button has been pressed
		if(bVal == "."){
			if(!calc.current){ calc.current += 0; }
			calc.current += ".";
		} else

		if(bVal == "="){

			var firstNumb = (calc.calculation.split(""))[0];

			var numbers = calc.calculation.match(/(\d+[.]\d+)|\d+/g),
				operation = calc.calculation.match(/[+]|[-]|[x]|[/]/g),
				result;
			
			if(firstNumb >= 0) {
				operation = ["+"].concat(operation);
			} else
			if(firstNumb === "-"){
				numbers[0] = -numbers[0];
			}

			console.log(numbers);
			console.log(firstNumb);
			console.log(operation);

			result = numbers.reduce(function(prev, curr, idx){
				console.log(calculate(prev,curr,operation[idx]));
				return calculate(prev,curr,operation[idx]);
			});

			console.log("result:"+result);

			calc.result = result;
			calc.memory = result;
			calc.current = "";

		}

		else {
			if(!calc.current){
				calc.memory += bVal;
			} else {
				calc.memory += calc.current + bVal;
			}
			calc.current = "";
		}
	});
	
});
              
            
!
999px

Console