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

              
                
	<body>
		<div class="calculator">
			<div class="calc-container">
				<div class="output">
					<div class="operation">
						<div class="value"></div>
					</div>
					<div class="result">
						<div class="value">0</div>
					</div>
				</div>
				<div class="input"></div>
			</div>
		</div>
	</body>
	<script src="calculator.js"></script>
</html>
              
            
!

CSS

              
                *{
    padding: 0;
    margin: 0;
    background-origin: padding-box;
    font-family: 'Poppins', sans-serif;
}

html{
    font-size: 16px;
    background-color: #636363;
}

.calculator{
    position: absolute;

    top: 50%;
    left: 50%;

    transform: translate(-50%, -50%);
}

.calc-container{
    background-color: #1d1d1d;

    width: 300px;
    height: auto;

    border-radius: 0 0 30px 30px;
}

.output{
    width: 100%;;
}   

.operation{
    width: 300px;
    height: 70px;

    color: #878787;

    font-size: 1.9em;
    font-weight: bold;

    position: relative;
    overflow: hidden;
}
.operation .value{
    position: absolute;

    bottom: 5px;
    right : 10px;
    
    white-space: nowrap;
    overflow: hidden;
}

.result{
    width: 300px;
    height: 140px;

    color: #FFF;

    font-size: 2.7em;
    font-weight: bold;
    
    overflow: hidden;
    position: relative;
}

.result .value{
    position: absolute;

    top: 50%;
    transform: translateY(-50%);
    right : 10px;
    
    white-space: nowrap;
    overflow: hidden;
}


.input{
    background-color: #FFF;

    border-radius: 0 0 25px 25px;
    border: 1px solid #1d1d1d;
    height: 340px;
}

.row {
    display: flex;
    justify-content: space-around;
    align-items: center;
}

.row button{
    width: 50px;
    height: 50px;

    font-size: 1.5em;

    border: none;
    border-radius: 50%;
    background-color: transparent;

    margin : 8px;

    cursor: pointer;
}

.row button:hover{
    font-weight: bold;
}

.row #calculate{
    color: #FFF;
    background-color: #46e0bc;
    width : 121px;
    border-radius: 50px;
}
.row #delete{
    color: #FFF;
    background-color: #e05046;
}

#division, #multiplication, #subtraction, #addition {
    color: #18b893;
}
              
            
!

JS

              
                let calculator_buttons = [
    {
        name : "delete",
        symbol : "⌫",
        formula : false,
        type : "key"
    },{
        name : "clear",
        symbol : "C",
        formula : false,
        type : "key"
    },{
        name : "percent",
        symbol : "%",
        formula : "/100",
        type : "number"
    },{
        name : "division",
        symbol : "÷",
        formula : "/",
        type : "operator"
    },{
        name : "7",
        symbol : 7,
        formula : 7,
        type : "number"
    },{
        name : "8",
        symbol : 8,
        formula : 8,
        type : "number"
    },{
        name : "9",
        symbol : 9,
        formula : 9,
        type : "number"
    },{
        name : "multiplication",
        symbol : "×",
        formula : "*",
        type : "operator"
    },{
        name : "4",
        symbol : 4,
        formula : 4,
        type : "number"
    },{
        name : "5",
        symbol : 5,
        formula : 5,
        type : "number"
    },{
        name : "6",
        symbol : 6,
        formula : 6,
        type : "number"
    },{
        name : "addition",
        symbol : "+",
        formula : "+",
        type : "operator"
    },,{
        name : "1",
        symbol : 1,
        formula : 1,
        type : "number"
    },{
        name : "2",
        symbol : 2,
        formula : 2,
        type : "number"
    },{
        name : "3",
        symbol : 3,
        formula : 3,
        type : "number"
    },{
        name : "subtraction",
        symbol : "–",
        formula : "-",
        type : "operator"
    },{
        name : "0",
        symbol : 0,
        formula : 0,
        type : "number"
    },{
        name : "comma",
        symbol : ".",
        formula : ".",
        type : "number"
    },{
        name : "calculate",
        symbol : "=",
        formula : "=",
        type : "calculate"
    }
];

// Select Elements
const input_element = document.querySelector(".input");
const output_result_element = document.querySelector(".result.value");
const output_operation_element = document.querySelector(".operation.value");

//Create Calculator Buttons
function createButtons(){
    const btns_per_row = 4;
    let added_btns = 0;

    calculator_buttons.forEach( (button, index) => {
        if(added_btns % btns_per_row == 0 ){
            input_element.innerHTML += `<div class="row"></div>`
        }

        const row = document.querySelector(".row:last-child");

        row.innerHTML += `<button id="${button.name}">
                            ${button.symbol}
                            </button>`;
        added_btns++;
    })
}

createButtons();

//Click Event
input_element.addEventListener("click", event => {
    const target_btn = event.target;

    calculator_buttons.forEach( button => {
        if(button.name == target_btn.id) calculator(button);
    })
})
//Calculator Data
let data = {
    operation : [],
    result : []
}


//Calculator
function calculator(button){
    if( button.type =="operator"){
        data.operation.push(button.symbol);
        data.result.push(button.formula);

    }
    else if(button.type =="number"){
        data.operation.push(button.symbol);
        data.result.push(button.formula);

    }
    else if(button.type =="key"){
        if(button.name =="clear" ){
            data.operation = [];
            data.result = [];
            updateOutputResult(0);

        }

    }
    else if(button.name == "delete"){
        data.operation.pop();
        data.result.pop();
    }
    else if(button.type =="calculate"){
        let join_result = data.result.join('');

        let result;
        try{
            result = eval(join_result);
        } catch (error){
            if( error instanceof SyntaxError){
                result = "Syntax Error!";
                updateOutputResult(result);
                return;
            }
        }

        result = formatResult(result);

        updateOutputResult(result);

        data.operation = [];
        data.result = [];

        data.operation.push(result);
        data.result.push(result);

        return;

    }
    updateOutputOperation(data.operation.join(''));
    
}
function updateOutputOperation(operation){
    output_operation_element.innerHTML = operation;
}
function updateOutputResult(result){
    output_result_element.innerHTML = result;
}

// Format Result
function formatResult(result){
    const max_output_number_length = 10;
    const output_precision = 5;

    if(digitCounter(result) > max_output_number_length){
        if(isFloat(result)){
            const result_int = parseInt(result);
            const result_int_lenght = digitCounter(result_int);

            if(result_int_length > max_output_number_length){
                return result.toPrecision(output_precision);
            }
            else{
                const num_of_digits_after_point = max_output_number_length - result_int_length;
                return result.toFixed(mum_of_digits_after_point)
            }

        }
        else{
            //if the number is an interger
            return result.toPrecision(output_precision);
        }

    }
    else{
        return result;
    }

}

// Digit Counter
function digitCounter(number){
    return number.toString().length;
}

//Check if a number is a float or not
function isFloat(number){
    return number % 1 !=0;
}
              
            
!
999px

Console