123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="root">
    <!-- This element's contents will be replaced with your component. -->
</div>
            
          
!
            
              @import 'https://fonts.googleapis.com/css?family=Share+Tech+Mono';
@font-face {
    font-family: "Digital";
    src: url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.eot");
    src: url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.eot?#iefix") format("embedded-opentype"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.woff2") format("woff2"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.woff") format("woff"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.ttf") format("truetype"), url("//db.onlinewebfonts.com/t/8e22783d707ad140bffe18b2a3812529.svg#Digital-7") format("svg");
  }
*{
    padding:0;
    margin: 0;
    box-sizing: border-box;
}


#root {
    display: flex;
    height: 100vh;
}

#calculator-body{
    margin: auto;
    border-radius: 10px;
    background-color: gray;
    width: 240px;
    text-align: center;
    font-family: digital;
    padding: 10px;
}

#calcOutput{
    background-color: white;
    height: 70px;
    font-size: 50px;
    text-align: end;
    overflow: hidden;
    border-radius: 5px;
    padding: 5px;
}

#calculator {
    margin-top: 10px;

}

#calculator .btn {
    margin: 5px;
    width: 35px;
    height: 35px;
}

#calculator .btn-wide {
    width: 170px;
}
            
          
!
            
              class Calculator extends React.Component { 
    constructor(props){
        super(props)
// if flag is true it means that one decimals are loaded or an operator, this is a good solution for the user story, 
// only the last operator is counted and only one decimals in the number
        this.state = {
            currentNumber: "0",
            operatorFlag: false,
            decimalFlag: false
        }

        this.handleClick = this.handleClick.bind(this);
    }
        handleClick(buttonName) {
        let currentNumber = this.state.currentNumber;
        let operatorFlag = this.state.operatorFlag;
        let decimalFlag = this.state.decimalFlag;
        //swithc is true until break
        switch(true){
        //in the cases of 0123456789 go through the loop, condition is current number is not a 0
          case  buttonName === "0" ||
                buttonName === "1" ||
                buttonName === "2" ||
                buttonName === "3" ||
                buttonName === "4" ||
                buttonName === "5" ||
                buttonName === "6" ||
                buttonName === "7" ||
                buttonName === "8" ||
                buttonName === "9" :
                if(this.state.currentNumber != "0"){
                //if condition is true add clicked case to current number
                    currentNumber += buttonName;
                    //switch operator flag to false to enable re-insertion of operator + - / *
                    operatorFlag = false;
                } else {
                //if the current number is 0 then override zero with pressed case
                    currentNumber = buttonName;
                }
                    break;
                  //in the case of + - / * check condition of operator flag if true 
                    case buttonName === "+" ||
                         buttonName === "-" ||
                         buttonName === "/" ||
                         buttonName === "*" :
                         if(!this.state.operatorFlag){
                           //add operator to number
                             currentNumber += buttonName;
                              //switch operator flag to true to disable re-insertion of operator + - / *
                             operatorFlag = true;
                             decimalFlag= false;
                          //if contidion false
                         }else{
                          //remove last entry from current number and add new entry 
                             const newNumber = currentNumber.slice(0,currentNumber.length-1);
                             currentNumber = newNumber + buttonName;
                         }
                        break;
                        case buttonName === "C":
                        currentNumber = "0";
                        operatorFlag = false;
                        decimalFlag= false;
                        break;
                        case buttonName === "=":
                        currentNumber = eval(currentNumber);
                        operatorFlag = false;
                        decimalFlag= true;
                        break;
                        case buttonName === ".":
                        if(!this.state.decimalFlag){
                            currentNumber += buttonName;
                            decimalFlag = true;
                        }
                } 
                this.setState({decimalFlag: decimalFlag});        
                this.setState({currentNumber:currentNumber});
                this.setState({operatorFlag: operatorFlag});
        }



    render() { 
        return (
            <div id = "calculator-body">
                <div id="calcOutput" >
                    <Screen currentNumber = {this.state.currentNumber}/>
                </div>
                <div id = "calculator">
                    <Button id = "clear" name = "C" handleClick = {this.handleClick} className = {"btn btn-danger"}/>
                    <Button id = "multiply" name = "*" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "divide" name = "/" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "add" name = "+" handleClick = {this.handleClick} className = {"btn btn-info"}/>

                    <Button id = "seven" name = "7" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "eight" name = "8" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "nine" name = "9" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "subtract" name = "-" handleClick = {this.handleClick} className = {"btn btn-info"}/>

                    <Button id = "four" name = "4" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "five" name = "5" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "six" name = "6" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "decimal" name = "." handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "one" name = "1" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "two" name = "2" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                    <Button id = "three" name = "3" handleClick = {this.handleClick} className = {"btn btn-info"}/>
                      
                    
                    <Button id = "equals" name = "=" handleClick = {this.handleClick} className = {"btn btn-info btn-high"}/>
                    <Button id = "zero" name = "0" handleClick = {this.handleClick} className = {"btn btn-info btn-wide"}/>
                    
            </div>
                
            </div>
            

        ); 
    } 
} 

class Screen extends React.Component {
    render(){
        return(
            <div id = "display">
                {this.props.currentNumber}
            </div>
        )
    }
}

class Button extends React.Component {
    runParentHandleClick = () => {
        this.props.handleClick(this.props.name)
    }
    render(){
        return(
            <button id = {this.props.id} onClick = {this.runParentHandleClick} className = {this.props.className}>{this.props.name}</button>
        )
    }
}
ReactDOM.render(<Calculator />, document.getElementById('root'));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console