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

              
                <html>
  <head>
    <script defer src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js">
    <meta name="author" content="Andres Tituana"></script>
  </head>
  <body id="body">
    
  </body> 
</html>

              
            
!

CSS

              
                :root {
  --display:black;
  --cuerpo:#0B344A;
  --botones:#60809f;
  --botones_sec:#4B647C;
  --fondo:rgb(10,25,50);
}

#body{
  margin:0px;
  background-color:var(--fondo);
  display:flex;
  justify-content:center;
  align-items:center;
  height:100vh;
  
}

#cuerpo{
  background-color:var(--cuerpo);
  display:flex;
  justify-content:center;
  align-items:center;
  height:70%;
  width:40%;
  min-width: 200px;
  flex-direction:column;
  padding:20px;
  border-radius: 5px;
}
#pantalla{
  width:100%;
  height:20%;
  border:5px solid var(--cuerpo);
  background-color:var(--display);
  border-radius: 8px;
}
#panel_buttons{
  /*background-color:red;*/
  background-color:var(--cuerpo);
  width:100%;
  height:80%;
  display:grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
}
#clear{
  grid-area:1/1/1/3;
}
#zero{
  grid-area:5/1/6/3;
}
#equals{
  grid-area:4/4/6/5;
}




.btns{
  background-color:var(--botones);
  border:1px solid var(--cuerpo);
  font-size:2em;
  border-radius: 5px;
  cursor:pointer;
  user-select: none;
  outline:none;
  
  display:flex;
  justify-content:center;
  align-items:center;
  
  font-family:"Impact", Gadget, sans-serif;
  font-weight:100;
  
}
.btns:hover {
  background-color: #0A3B40;
  color:white;
  border:1px solid black;
}

.result{
  background-color:var(--display);
  font-family:"Lucida Sans Unicode", Gadget, sans-serif;
  color: white;
  width:100%;
  height:50%;
  display:flex;
  justify-content:flex-end;
  align-items:center;
  border-radius: 8px;
}

.color{
  background-color:var(--botones_sec);
}
              
            
!

JS

              
                class Calculadora extends React.Component {
  constructor(props){
    super(props)
    this.state={
      result:false,
      temp_display:"0",
      current_display:""}
    this.insertar_num = this.insertar_num.bind(this);
    this.clean = this.clean.bind(this);
    this.insertar_char = this.insertar_char.bind(this);
    this.solve = this.solve.bind(this);
  }
  
  clean(){
    document.getElementById("display").style.color = "white";
      this.setState({
        temp_display:"0",
        current_display:""
      })
  }
  
  solve(){
    let cleaner_regex = /(?<=\d)\D+$/gi;
    this.setState((state, props)=>({
      current_display: state.current_display.replace(cleaner_regex, "")
    }))
    let sum_search_regex = /\-?\d+(\.\d+)?((\*|\/)\-?\d+(\.\d+)?)*/gi;
    let sumas = this.state.current_display.match(sum_search_regex)
    
    let mult_search_regex = /\-?\d+(\.\d+)?|[\/,\*]/gi;
    let sol = sumas.reduce((total, currentvalue)=>{
      let temp = currentvalue.match(mult_search_regex);
      let valuetemp = parseFloat(temp[0]);
      for(let a = 2; a<temp.length; a+=2){
        if(temp[a-1]=="*"){
          valuetemp *= parseFloat(temp[a]);
        }else{
          valuetemp /= parseFloat(temp[a]);
        }
      }
      return valuetemp+total;
    }, 0);
    this.setState((state, props)=>({
      temp_display:String(sol),
      result:true,
    }));
    document.getElementById("display").style.color = "yellow";
  }
  
  insertar_char(){
    if(this.state.result){ 
      this.setState((state, props)=>({
        result:false,
        temp_display:"0",
        current_display:state.temp_display
      }));
    }
    let regex = /(\/+)|(\++)|(\*+)|(\-+)/gi;
    let a = event.target.value;
    let sym = this.state.current_display.substring(this.state.current_display.length-1,this.state.current_display.length);
    let sym2 = this.state.current_display.substring(this.state.current_display.length-2,this.state.current_display.length);
    if(this.state.current_display!=""){
    if(/(\/+)|(\++)|(\*+)/gi.test(sym) && /(\-+)/gi.test(a)){
       this.setState((state,props)=>({
        current_display:state.current_display.concat(a),
        temp_display:""
        }))
    }
      else if(/((\/+)|(\++)|(\*+))-/gi.test(sym2)&&/(\/+)|(\++)|(\*+)/gi.test(a)){
        this.setState((state,props)=>({
          current_display:state.current_display.substring(0, state.current_display.length - 2).concat(a),
          temp_display:""
          }))

        }
      else if(regex.test(sym)){
      this.setState((state,props)=>({
          current_display:state.current_display.substring(0, state.current_display.length - 1).concat(a),
          temp_display:""
          }))
        }
      else{
          this.setState((state,props)=>({
          current_display:state.current_display.concat(a),
          temp_display:""
          }))
        }
    }
    
  }
  
  insertar_num(event){
    if(this.state.result){
      
      this.clean();
      this.setState({
        result:false,
        temp_display:""
      });
    }else if(this.state.temp_display=="0"){
      this.setState({
        temp_display:""
      });
    }
    
    let punto_regex = /\.+/gi;
    let a = event.target.value;
    let notienepunto = !punto_regex.test(this.state.temp_display);
    if(a=="."){
       if(notienepunto){
         if(this.state.temp_display =="0"){
            this.setState((state,props)=>({
              temp_display:"0.",
              current_display:state.current_display.concat("0.")
            }))
          }else{
            this.setState((state,props)=>({
              temp_display:(state.temp_display.concat(a)),
              current_display:state.current_display.concat(a)
            }))
          }
       }
    }else{
       this.setState((state,props)=>({
        temp_display:(state.temp_display.concat(a)),
         current_display:state.current_display.concat(a)
      }))
    }
      
   
    
  }
  
  render(){
    return(
    <div id="cuerpo">
      <div id="pantalla">
        <div className="result">{this.state.current_display}</div>
        <div className="result" id="display">{this.state.temp_display}</div>
      </div>
      <div id="panel_buttons">
        <button id="clear" className="btns color" onClick={this.clean}>ac</button>
        <button id="divide" className="btns color" onClick={this.insertar_char} value={"/"}>/</button>
        <button id="multiply" className="btns color" onClick={this.insertar_char} value={"*"}>*</button>
        <button id="seven" className="btns" onClick={this.insertar_num} value={7} >7</button>
        <button id="eight" className="btns" onClick={this.insertar_num} value={8}>8</button>
        <button id="nine" className="btns" onClick={this.insertar_num} value={9}>9</button>
        <button id="subtract" className="btns color" onClick={this.insertar_char} value={"-"}>-</button>
        <button id="four" className="btns" onClick={this.insertar_num} value={4}>4</button>
        <button id="five" className="btns" onClick={this.insertar_num} value={5}>5</button>
        <button id="six" className="btns" onClick={this.insertar_num} value={6}>6</button>
        <button id="add" className="btns color" onClick={this.insertar_char} value={"+"}>+</button>
        <button id="one" className="btns" onClick={this.insertar_num} value={1}>1</button>
        <button id="two" className="btns" onClick={this.insertar_num} value={2}>2</button>
        <button id="three" className="btns" onClick={this.insertar_num} value={3}>3</button>
        <button id="equals" className="btns color" onClick={this.solve}>=</button>
        <button id="zero" className="btns" onClick={this.insertar_num} value={0}>0</button>
        <button id="decimal" className="btns" onClick={this.insertar_num} value={"."}>.</button>

      </div>
    </div>
    
    );
  }
}

ReactDOM.render(<Calculadora/>, document.getElementById("body"));


              
            
!
999px

Console