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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div class="container">
<div class="insideContainer">
    <div class="row">
        <h5 class="title">Javascript Calculator</h5>
    </div>
    <div class="row row-centered">
        <div class="col-md-8 screen" id="value"></div>
    </div>  <!-- end of row -->
        <div class="row row-centered">
            <div class="col-md-2 key col-centered" id="clearAll">AC</div>
            <div class="col-md-2 key col-centered" id="backspace">CE</div>
            <div class="col-md-2 op col-centered" id="negativeSign">+/-</div>
            <div class="col-md-2 op col-centered" id="percent">%</div>
        </div>  <!-- end of row -->
    <div class="row row-centered">
        <div class="col-md-2 key col-centered" data-num="7">7</div>
        <div class="col-md-2 key col-centered" data-num="8">8</div>
        <div class="col-md-2 key col-centered" data-num="9">9</div>
        <div class="col-md-2 op col-centered" data-op="/">&divide;</div>
    </div>  <!-- end of row -->
    <div class="row row-centered">
        <div class="col-md-2 key col-centered" data-num="4">4</div>
        <div class="col-md-2 key col-centered" data-num="5">5</div>
        <div class="col-md-2 key col-centered" data-num="6">6</div>
        <div class="col-md-2 op col-centered" data-op="*">&#215;</div>
    </div>  <!-- end of row -->
    <div class="row row-centered">
        <div class="col-md-2 key col-centered" data-num="3">3</div>
        <div class="col-md-2 key col-centered" data-num="2">2</div>
        <div class="col-md-2 key col-centered" data-num="1">1</div>
        <div class="col-md-2 op col-centered" data-op="-">&minus;</div>
    </div>  <!-- end of row -->
    <div class="row row-centered">
        <div class="col-md-2 key col-centered" data-num="0">0</div>
        <div class="col-md-2 key col-centered" data-num=".">&#46;</div>
        <div class="col-md-2 op col-centered"id="equals">&#61;</div>
        <div class="col-md-2 op col-centered" data-op="+">&plus;</div>
    </div>  <!-- end of row -->
	</div> <!-- end of insideContainer -->
</div>  <!-- end of container -->

              
            
!

CSS

              
                /*
offwhite - #eaedc3
olive - #809a6a
MAIN: BLUE GREY #607d8b (500)
2nd: #455a64 (700)
#90a4ae (300)

ACCENT: TEAL #00BFA5 (A700)

*/
/* main colors to use */
:root {
    --main-color: #607d8b;
    --secondary-color: #455a64;
    --accent-color: #00bfa5;
    --bk-color: #90a4ae;
    --white: #eaedc3;
}
/* body style*/
body {
    background-color: var(--bk-color);
    font-family: 'Proza Libre', sans-serif;
	  margin: 0 auto;
}

.title {
    color: #eaedc3;
    font-size: 40px;
	  margin-top: .5em;
	  padding: .5em;
    border: 1px solid black;
	  border-radius: .25em;
	  box-shadow: 0 8px 6px -6px black;
}

/* key style*/
.key {
    background-color: var(--secondary-color);
    color: #eaedc3;
    border: .015em solid #CFD8DC;
    text-align: center;
    justify-content: center;
    font-size: 1.5em;
    align-items: center;
    width: 25%;
    height: 75px;
    cursor: pointer;
    /*text-shadow: 1px 1px 2px var(--accent-color);*/
}

.op {
    background-color: #78909C;
    color: #eaedc3;
    border: .015em solid #CFD8DC;
    text-align: center;
    justify-content: center;
    font-size: 1.5em;
    align-items: center;
    width: 25%;
    height: 75px;
    cursor: pointer;
  
    /*text-shadow: 1px 1px 2px var(--accent-color);*/
}

.key:hover {
    background-color: var(--main-color);
    color: #eaedc3;
    border: .015em solid #CFD8DC;
    text-align: center;
    justify-content: center;
    font-size: 1.5em;
    align-items: center;
    /*text-shadow: 1px 1px 2px var(--accent-color);*/
}

.op:hover {
    background-color: var(--accent-color);
    color: #eaedc3;
    border: .015em solid #CFD8DC;
    text-align: center;
    justify-content: center;
    font-size: 1.5em;
    align-items: center;
    /*text-shadow: 1px 1px 2px var(--accent-color);*/
}

.row-centered {
    text-align: center;
}

.col-centered {
    display: inline-block;
    float: none;
}

.screen {
    background-color: var(--main-color);
    color: #eaedc3;
    text-align: right;
    font-size: 1.5em;
	  position: relative;
    height: 75px;
    border: .015em solid #CFD8DC;
		margin-top: 1em;
}

.insideContainer {
	margin: 0 auto;
}
              
            
!

JS

              
                const numbers = document.querySelectorAll('[data-num]');
const operations = document.querySelectorAll('[data-op]');
const screen = document.querySelector('#value');
const clearAll = document.querySelector('#clearAll');
const backspace = document.querySelector('#backspace');
const equalSign = document.querySelector("#equals");
const percent = document.querySelector('#percent');
const negativeSign = document.querySelector("#negativeSign");

// store current runningInput
let runningInput;
//stores current inputs
let inputs =[""];
let total = 0;
let isNeg = false;

//array used to validate input
const op1 = ['+','-','x','/','='];
const op2 = ['.'];
const num = [0,1,2,3,4,5,6,7,8,9];

function getVal(input) {

}

function update() {
   // console.log("1 "+runningInput);
    runningInput =  inputs.join();
    const regex = new RegExp(',', 'g');
    runningInput = runningInput.replace(regex,'');
    //console.log("2 "+runningInput);
    screen.textContent = runningInput;
}

function getTotal() {
    runningInput = inputs.join();
    const regex = new RegExp(',', 'g');
    runningInput = runningInput.replace(regex, '');
    total = eval("(" + runningInput + ")");
		//inputs.splice(0,inputs.length,total);
    //update();
    //console.log(total);
    screen.textContent = total;
}

//listens and gets the clicked on number
numbers.forEach(number => number.addEventListener('click', () => {
    inputs.push(number.dataset.num);
    update();
    //screen.textContent += number.dataset.num; // adds to textContent
    //currentNumber += number.dataset.num; //set this to current number
    //currentNumber.slice(1);
    //console.log(currentNumber);
}));

// listens for clicks on backspace button
backspace.addEventListener('click', () => {
    //screen.textContent = screen.textContent.substr(0,screen.textContent.length-1);
    //console.log("I'm a backspace");
    inputs.pop();
    update();
    //screen.textContent = screen.textContent.substr(0,screen.textContent.length-1);
});

//listens for click on clearAll button
clearAll.addEventListener('click', () => {
    //currentNumber = 0;
    inputs = [""];
    update();
    screen.textContent = 0;
});

// gets operator clicked on, and sets to current operator
operations.forEach(operation => operation.addEventListener('click', () => {
    inputs.push(operation.dataset.op);
    update();
    //currentOperator = operation.dataset.op;
   // screen.textContent = getOPSymbol(currentOperator);
    //console.log(currentOperator);
}));

//gets total when equal sign is clicked on
equalSign.addEventListener('click', getTotal);

// listens for click on % button and changes number to a %
percent.addEventListener('click', () => {
    inputs.splice(inputs.length-2,0,'.');
    update();
});

negativeSign.addEventListener('click', () => {
    if (!isNeg) {
        inputs.splice(0,0,'-');
        update();
        isNeg = true;
    } else {
        inputs.splice(0,1);
        update();
        isNeg = false;
    }
});

              
            
!
999px

Console