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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<div id="root"></div>
              
            
!

CSS

              
                *{
    box-sizing: border-box;
}
body{
    margin: 0px;
    padding: 0px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
    background-image: linear-gradient(to top, #0e54be, #2c6acd, #4580db, #5d97e8, #76adf4);
}
#calculator{
    width: 500px;
    height: 312px;
    background-color: #fcfcfd63;
    display: flex;
    flex-direction: column;
    border: 1px solid white;
}
#display{
    display: flex;
    flex-direction: row-reverse;
    padding: 0px 10px 0px 10px;
    height: 23%;
    background-color: black;
    color: aqua;
    overflow: hidden;
}
#input-container{
    display: flex;
}
#inputs{
    display: flex;
    flex-wrap: wrap-reverse;
    width: 60%;
}
#inputs div{
    padding: 20px;
    display: flex;
    justify-content: center;
    cursor: pointer;
    color: #88ace6;
    background-color: #020202a8;
    border: 1px solid #88ace6;
    font-weight: 900;
}
#decimal{
    flex: 2;
}
#decimal, #one, #two, #three, #four, #five, #six, #seven, #eight, #nine{
    width: 33.3%;
}
#zero{
    width: 66.6%;
}
#controls{
    width: 40%;
    display: flex;
    flex-wrap: wrap-reverse;
}
#controls div{
    width: 33.3%;
    background-color: rgba(0, 0, 0, 0.849);
    border: 1px solid #88ace6;
    color: #88ace6;
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    font-weight: bold;
}
#clear{
    background-color: #972828!important;
}
#equals{
    background-color: #1e5318!important;
}
              
            
!

JS

              
                let firstTime = true;
let operator = "";
let useDot = true;
let replace = true;
class Calculator extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            display: 0,
            memory: 0
        }
        this.onClicked = this.onClicked.bind(this);
    }
    onClicked = ( item ) => {
        if(item === "+" || item === "-" || item === "x" || item === "/"){
            if(firstTime){ // if this is the first time, dont calculate
                this.setState({ memory: this.state.display });
                this.setState({ display: 0 });
                firstTime = false;
                operator = item;
                useDot = true;
            }else{ // not the first time, calculate
                const n1 = parseFloat(this.state.memory);
                const n2 = parseFloat(this.state.display);
                if(operator === "+"){this.setState({ memory: n1 + n2 })}
                if(operator === "-"){this.setState({ memory: n1 - n2 })}
                if(operator === "x"){this.setState({ memory: n1 * n2 })}
                if(operator === "/"){this.setState({ memory: n1 / n2 })}
                operator = item;
                this.setState({ display: 0 });
            }
            replace = true;
        }else if(item === "AC"){
            this.setState({ display: 0, memory: 0 });
            operator = "";
            firstTime = true;
            useDot = true;
            replace = true;
        }else if(item === "."){
            if(useDot){
                this.setState({ display: "" + this.state.display + item });
                useDot = false;
                replace = false;
            }
        }else if(item === "="){
            const current = parseFloat(this.state.display);
            const memory = parseFloat(this.state.memory);
            if(operator === "+"){this.setState({ display: memory + current })}
            if(operator === "-"){this.setState({ display: memory - current })}
            if(operator === "x"){this.setState({ display: memory * current })}
            if(operator === "/"){this.setState({ display: memory / current })}
            this.setState({ memory: 0 });
            operator = "";
            firstTime = true;
            useDot = true;
            replace = true;
        }else{ // if numbers pressed do these
            const check = "" + this.state.display;
            if( replace === false ){
                if(Number(check) === 0){
                    this.setState({ display: item });
                }else{
                    this.setState({ display: "" + this.state.display + item });
                }
            }
            if( check.includes(".") ){ 
                this.setState({ display: "" + this.state.display + item });
                replace = false;
            }
            if( Number(check) === 0 && replace === true){ 
                this.setState({ display: item });
                replace = false;
            }
        }
    }
    render () {
        return (
            <div id="calculator">
                <Display 
                    display={this.state.display}
                />
                <div id="input-container">
                    <Inputs 
                        onClicked={this.onClicked}
                    />
                    <Controls
                        onClicked={this.onClicked}
                    />
                </div>
            </div>
        );
    }
}
function Display ({ display }) {
    return (
        <div id="display">
            <h2>{display}</h2>
        </div>
    );
}
function Inputs ({ onClicked }) {
    const inputs = [
        {
            id: "zero",
            text: 0
        },
        {
            id: "decimal",
            text: "."
        },
        {
            id: "one",
            text: 1
        },
        {
            id: "two",
            text: 2
        },
        {
            id: "three",
            text: 3
        },
        {
            id: "four",
            text: 4
        },
        {
            id: "five",
            text: 5
        },
        {
            id: "six",
            text: 6
        },
        {
            id: "seven",
            text: 7
        },
        {
            id: "eight",
            text: 8
        },
        {
            id: "nine",
            text: 9
        }
    ];
    return (
        <div id="inputs">
            {inputs.map(input => 
                <div 
                    id={input.id}
                    onClick={() => onClicked(input.text)}
                >
                    {input.text}
                </div>
            )}
        </div>
    );
}
function Controls ({ onClicked }) {
    const ctrs = [
        {
            id: "add",
            text: "+"
        },
        {
            id: "subtract",
            text: "-"
        },
        {
            id: "equals",
            text: "="
        },
        {
            id: "multiply",
            text: "x"
        },
        {
            id: "divide",
            text: "/"
        },
        {
            id: "clear",
            text: "AC"
        }
    ];
    return (
        <div id="controls">
            {ctrs.map( item => 
                <div 
                    id={item.id}
                    onClick={() => onClicked(item.text)}
                >
                    {item.text}
                </div>
            )}
        </div>
    );
}

ReactDOM.render(
    <Calculator />,
    document.getElementById('root')
);
              
            
!
999px

Console