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 id="root"></div>

              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Open+Sans:300,400,600|Roboto:100');
* {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans', sans-serif;
  text-shadow: rgba(0,0,0,.01) 0 0 1px;
  background: url('https://www.dropbox.com/s/ebqn64oj05d1jli/bg.jpg?raw=1');
  background-size: cover;
}
#root {
  display: flex;
  justify-content: center;
  height: 100vh;
  align-items: center;
}
button {
  width: 100px;
  height: 85px;
  background: #e6e6e6;
  border: solid 0.5px #9e9e9e;
  font-size: 1.9em;
  outline: none;
  cursor: pointer;
}
button:active{
  background: #b2b1b2;
  border-color: #b2b1b2;
}
.toggles {
  background: rgb(220, 220, 220);
}
.toggles:active{
  background: #b2b1b2;
}
.operators {
  background: #f5923d; 
  color: #fff;
  font-family: Roboto;
  font-weight: 600;

}
.operators:active {
  background: #b2b1b2;
}
.calculator {
  width: 400px;
  border-radius: 10px;
  overflow: hidden;
  border: solid 0.5px #00000063;
  box-shadow: 0 37.125px 70px -12.125px rgba(0,0,0,0.5);
}
.outputScreen{
  text-align: right;
  padding-right: 20px;
  padding-left: 20px;
  background: #4c4c4cf2;
  height: 140px;
}
.result {
  color: #f5f8fa;
  font-size: 5.2em;
  font-weight: 100;
  letter-spacing: 2px;
  font-family: Roboto;
  position: relative;
}
.circles {
  text-align: left;
  margin-bottom: 20px;
}
.red {
  background: #fc5753;
  padding: 0px 11px 0px 11px;
  border-radius: 20px;
  position: relative;
  top: 10px;
  margin-right: 12px;
  cursor: pointer;
}
.red:active {
  background: #e93d3a;
}
.yellow {
  background: #fcbc40;
  padding: 0px 11px 0px 11px;
  border-radius: 20px;
  position: relative;
  top: 10px;
  margin-right: 12px;
  cursor: pointer;
}
.yellow:active {
  background: #d39c2f;
}
.green {
  background: #33c84a;
  padding: 0px 11px 0px 11px;
  border-radius: 20px;
  position: relative;
  top: 10px;
  margin-right: 12px;
  cursor: pointer;
}
.green:active {
  background: #2db641;
}
.the-guy {
  position: relative;
  top: 70px;
  text-align: center;
  color: #fff;
  background: #00000066;
  padding: 10px;
  border-radius: 10px;
  width: 200px;
  margin: 0 auto;
}
.the-guy a {
  color: #fff;
}
              
            
!

JS

              
                const zeroStyle = { width: 200 };
var fonts = '';
var pos = 0;
var size = {
    8: "4.5em-10",
    9: "4.0em-20",
    10: "3.5em-30",
    11: "3.2em-35",
    12: "2.9em-40",
    13: "2.6em-45",
    14: "2.4em-50",
    15: "2.3em-50",
    16: "2.1em-53",
    17: "2.0em-56",
    18: "1.9em-58",
    19: "1.72em-60",
    20: "1.5em-62",
    21: "1.5em-62",
    22: "1.5em-62"
};

class Buttons extends React.Component {
    render() {
        return (
            <div style={{ opacity: 0.96 }}>

                <div>
                    <button id="clear" className="toggles" value='AC' onClick={this.props.initialize}>{this.props.initText}</button>
                    <button id="add-sub" className="toggles" value='+/-' onClick={this.props.togglesub}>+/-</button>
                    <button id="precent" className="toggles" value='%' onClick={this.props.percent}>%</button>
                    <button id="divide" className="operators" value='/' onClick={this.props.operators}>&divide;</button>
                </div>

                <div>
                    <button id="seven" value='7' onClick={this.props.numbers} >7</button>
                    <button id="eight" value='8' onClick={this.props.numbers} >8</button>
                    <button id="nine" value='9' onClick={this.props.numbers} >9</button>
                    <button id="multiply" className="operators" value='x' onClick={this.props.operators}>&times;</button>
                </div>

                <div>
                    <button id="four" value='4' onClick={this.props.numbers} >4</button>
                    <button id="five" value='5' onClick={this.props.numbers} >5</button>
                    <button id="six" value='6' onClick={this.props.numbers} >6</button>
                    <button id="subtract" className="operators" value='sub' onClick={this.props.operators}>&minus;</button>
                </div>

                <div>
                    <button id="one" value='1' onClick={this.props.numbers} >1</button>
                    <button id="two" value='2' onClick={this.props.numbers} >2</button>
                    <button id="three" value='3' onClick={this.props.numbers} >3</button>
                    <button id="add" value='+' className="operators" onClick={this.props.operators}>&#43;</button>
                </div>

                <div>
                    <button id="zero" value='0' onClick={this.props.numbers} style={zeroStyle}>0</button>
                    <button id="decimal" value='.' onClick={this.props.decimal} >.</button>
                    <button id="equals" value='=' className="operators" onClick={this.props.evaluate}>&#61;</button>
                </div>

            </div>
        );
    }
}

class Output extends React.Component {
    constructor(props) {
        super(props);

        this.handleFormat = this.handleFormat.bind(this);
        this.handleFontSize = this.handleFontSize.bind(this);
    }

    handleFormat(num) {
        var decimal = '';
        if (num.includes('.')) {

            decimal = num.split('.');
            num = decimal[0];
        }
        if (num.length > 3) {
            for (let i = num.length - 3; i > 0; i -= 3) {
                num = num.substr(0, i) + ',' + num.substr(i);
            }
        }
        if (decimal !== '') {
            return num + '.' + decimal[1];
        }
        return num;
    }

    handleFontSize(number) {
        var len = number.length;
        if (len < 8) {
            fonts = 5.2 + 'em';
            pos = 0;
        } else {
            const numbers = size[len].split('-');
            fonts = numbers[0];
            pos = parseInt(numbers[1]);
        }
    }

    render() {
        this.handleFontSize(this.props.result || this.props.secondVal || this.props.firstVal);
        return (
            <div id="display" className="outputScreen">
                <div className="circles">
                    <span className="red"></span>
                    <span className="yellow"></span>
                    <span className="green"></span>
                </div>
                <span className="result" style={{ fontSize: fonts, top: pos }}>
                    {isNaN(this.props.result) ? 'Error' : this.props.result === 'Infinity' ? 'Error' : this.handleFormat(this.props.result || this.props.secondVal || this.props.firstVal)}
                </span>
            </div>
        );
    }
};

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            firstVal: '0',
            secondVal: '',
            currentSign: '',
            result: '',
            prevSign: '',
            prevResult: '',
            prevSecondVal: '',
            initText: 'AC',
            display: ''
        }
        this.handleNumbers = this.handleNumbers.bind(this);
        this.handleOperators = this.handleOperators.bind(this);
        this.handleEvaluate = this.handleEvaluate.bind(this);
        this.handleDecimal = this.handleDecimal.bind(this);
        this.toggleSub = this.toggleSub.bind(this);
        this.initialize = this.initialize.bind(this);
        this.handlePercent = this.handlePercent.bind(this);
    }
    handleNumbers(e) {
        this.setState({
            result: ''
        })
        if (this.state.currentSign === '') {

            if (this.state.firstVal === '0' && e.target.value !== '0') {

                this.setState({
                    firstVal: e.target.value,
                    initText: 'C'
                })
            } else if (this.state.firstVal !== '0' && this.state.firstVal.length < 21) {

                this.setState({
                    firstVal: this.state.firstVal.concat(e.target.value),
                    initText: 'C'
                })
            }
        } else {
            if (this.state.secondVal === '' && e.target.value !== '0') {
                this.setState({
                    secondVal: e.target.value,
                    initText: 'C'
                })
            } else if (this.state.secondVal !== '' && this.state.secondVal.length < 21) {
                this.setState({
                    secondVal: this.state.secondVal.concat(e.target.value),
                    initText: 'C'
                })
            }
        }
    }
    handleOperators(e) {
        this.setState({
            currentSign: e.target.value
        })
        if (this.state.prevSign !== '' && this.state.result !== '') {
            this.setState({
                prevSign: '',
                currentSign: e.target.value,
                firstVal: this.state.result
            })
        }
    }

    handleEvaluate() {
        if (this.state.currentSign || this.state.prevSign) {
            function parser(x) {
                if (x.includes('.')) {
                    return parseFloat(x);
                } else {
                    return parseInt(x);
                }
            }
            var answer = {
                '+': function (x, y) { return parser(x) + parser(y) },
                'sub': function (x, y) { return parser(x) - parser(y) },
                'x': function (x, y) { return parser(x) * parser(y) },
                '/': function (x, y) { return parser(x) / parser(y) }
            }
            if (this.state.prevSign !== '') {
                this.setState({
                    result: answer[this.state.prevSign](this.state.result, this.state.prevSecondVal).toString(),
                })
            } else if (this.state.firstVal !== '0' && this.state.secondVal === '' && this.state.currentSign) {
                this.setState({
                    secondVal: this.state.firstVal,
                })
            } else {
                this.setState({
                    result: answer[this.state.currentSign](this.state.firstVal, this.state.secondVal).toString(),
                    prevResult: answer[this.state.currentSign](this.state.firstVal, this.state.secondVal).toString(),
                    prevSign: this.state.currentSign,
                    prevSecondVal: this.state.secondVal,
                    firstVal: '0',
                    secondVal: '',
                    currentSign: '',
                })
            }
        }
    }

    handleDecimal() {
        if (this.state.result !== '') {
            this.setState({
                result: ''
            })
        }
        if (this.state.currentSign === '') {


            if (!this.state.firstVal.includes('.')) {
                this.setState({
                    firstVal: this.state.firstVal.concat('.')
                })
            }
        } else if (this.state.currentSign !== '') {
            if (!this.state.secondVal.includes('.')) {
                this.setState({
                    secondVal: this.state.secondVal.concat('.')
                })
            }
        }

    }

    toggleSub() {
        var value = '';
        if (this.state.currentSign === '') {

            if (this.state.firstVal.indexOf('-') !== 0) {
                value = '-' + this.state.firstVal;
                this.setState({
                    firstVal: value
                })
            } else {
                value = this.state.firstVal.replace('-', '');
                this.setState({
                    firstVal: value
                })
            }
        } else {
            if (this.state.secondVal.indexOf('-') !== 0) {
                value = '-' + this.state.secondVal;
                this.setState({
                    secondVal: value
                })
            } else {
                value = this.state.secondVal.replace('-', '');
                this.setState({
                    secondVal: value
                })
            }
        }
    }

    handlePercent() {
        if (this.state.result !== '') {
            this.setState({
                result: (this.state.result / 100).toString()
            })
        } else if (this.state.currentSign === '') {
            this.setState({
                firstVal: (this.state.firstVal / 100).toString()
            })
        } else if (this.state.currentSign !== '') {
            if (this.state.currentSign === '+') {
                this.setState({
                    secondVal: (this.state.firstVal * this.state.secondVal / 100).toString()
                })
            } else if (this.state.currentSign === 'sub') {
                this.setState({
                    secondVal: (this.state.firstVal * this.state.secondVal / 100).toString()
                })
            } else {
                this.setState({
                    secondVal: (this.state.secondVal / 100).toString()
                })
            }
        }
    }

    initialize() {
        this.setState({
            firstVal: '0',
            secondVal: '',
            currentSign: '',
            result: '',
            prevSign: '',
            prevResult: '',
            prevSecondVal: '',
            initText: 'AC'
        })
    }

    render() {
        return (
            <div>
                <div className='calculator'>
                    <Output firstVal={this.state.firstVal}
                        secondVal={this.state.secondVal}
                        result={this.state.result} />
                    <Buttons evaluate={this.handleEvaluate}
                        operators={this.handleOperators}
                        initialize={this.initialize}
                        decimal={this.handleDecimal}
                        numbers={this.handleNumbers}
                        togglesub={this.toggleSub}
                        initText={this.state.initText}
                        percent={this.handlePercent} />
                </div>
                <div className="the-guy">By <a target="_blank" rel="noopener noreferrer" href="https://youtube.com/farhankk360/">Farhan</a>
                </div>
            </div>
        );
    }
}

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

Console