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/css2?family=VT323&display=swap');

body {
  margin: 0;
  display: grid;
  place-content: center;
  background-color: #dddddd;
  box-sizing: border-box;
  height: 100vh;
  font-family: Arial, sans-serif;
}

.container {
  background-color: white;
  box-shadow: rgba(50, 50, 93, 0.25) 0px 50px 100px -20px, rgba(0, 0, 0, 0.3) 0px 30px 60px -30px, rgba(10, 37, 64, 0.35) 0px -2px 6px 0px inset;
  border: 3px solid #666666;
  width: 500px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: minmax(100px, auto);
  grid-template-areas: 
    "lcd     lcd     lcd     lcd"
    "clear   clear   clear   op"
    "num     num     num     op"
    "num     num     num     op"
    "num     num     num     op"
    "zero    zero    dec     equals";
  
}

#clear {
  grid-area: clear;
}

#display {
  grid-area: display;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 10px;
  text-align: right;
  font-family: 'VT323', sans-serif;
  font-size: 3em;
  color: green;
  background-color: black;
}

#operations {
  grid-area: op;
  display: flex;
  flex-direction: column;
  margin: 0;
}

.op {
  grid-area: op;
}

#nums {
  grid-area: num;
  display: grid;
  grid-template-columns: repeat(3, calc(100% / 3));
  margin: 0;
}

#zero {
  grid-area: zero;
}

#decimal {
  grid-area: dec;
}

#equals {
  grid-area: equals;
}

.cell {
/*   border: 1px solid red; */
  min-height: 50px;
  display: grid;
  place-content: center;
  box-shadow: inset 0px 0px 2px #c1c1c1;
  background-color: gray;
  color: yellow;
  font-size: 1.5em;
}

.cell:active {
  background-color: black;
  color: white;
}

#readout {
  grid-area: readout;
  min-height: 30px;
  place-content: center;
  background-color: white;
  color: gray;
  
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 10px;
  text-align: right;
  font-family: 'VT323', sans-serif;
  font-size: 1.5em;
}

#lcd {
  grid-area: lcd;
}


              
            
!

JS

              
                import * as React from "https://cdn.skypack.dev/react@17.0.1";
import * as ReactDOM from "https://cdn.skypack.dev/react-dom@17.0.1";

const numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];

const operations = [
  {
    id:"add",
    operator: "+"
  },
  {
    id:"subtract",
    operator: "-"
  },
  {
    id:"multiply",
    operator: "x"
  },
  {
    id:"divide",
    operator: "÷"
  }
];

const Tapper = ({element, buttonID, calculate}) => {
  return (
    <div 
      className="cell" 
      onClick={calculate} 
      value={element}
      id={buttonID}>{element}
    </div>
  )
}

const Readout = ({ content, opChain }) => {
  return (
    <div id="lcd">
      <div id="readout">
         { opChain }
      </div>
      <div id="display">
         { content }
      </div>
    </div>
  );
}

const App = () => {

  const [input, setInput] = React.useState("");
  const [numberwang, setNumberwang] = React.useState("");
  
  const calculate = (e) => {
    let buttonPushed = e.target.innerText;
    let inputArr = input.split(" ");
    let lastArr = inputArr[inputArr.length-1]
    const rounding = 10000000;
    
    switch (true) {
      case ("1234567890".includes(buttonPushed)):
        // if the last item in the array is a number followed by a period, add the number to the end of that and reassemble the string
        if (/[0-9]\./.test(lastArr)) {
          setInput(`${input}${buttonPushed}`);
        } else if (parseFloat(lastArr) == lastArr) {
          if (inputArr.length === 1) {
            const newNum = parseFloat(`${input}${buttonPushed}`);
            setInput(`${newNum}`);
          } else {
            const firstArr = inputArr.slice(0,inputArr.length-1).join(" ");
            const secondArr = parseFloat(`${lastArr}${buttonPushed}`);
            const newNum = `${firstArr} ${secondArr}`;
          setInput(`${newNum}`);
          }
        } else {
          setInput(`${input} ${buttonPushed}`);
        }
        break;

      case ("+-x÷".includes(buttonPushed)):        
        if ("+-x÷".includes(lastArr)) {
          // 5 * - + 5 should equal 10
          if (buttonPushed==="-") {
            setInput(`${input} ${buttonPushed}`);
          } else {
            if (lastArr === "-") {
              // strip off the last symbols
              // add the new symbol
              const noWhiteSpaceInput = input.replaceAll(" ", "");
              const splitArr = noWhiteSpaceInput.split(/([0-9]+)/g);
              const newSlice = splitArr.slice(1, splitArr.length-1).join(" ");
              setInput(`${newSlice} ${buttonPushed}`);
              
            } else {
              const arrSlice = inputArr.slice(0,inputArr.length-1);
              const newSlice = `${arrSlice} ${buttonPushed}`;
              setInput(newSlice);
            }
          }
        } else {
          setInput(`${input} ${buttonPushed}`);
        }
        break;
        
      case (buttonPushed=="="):
        const replaceDiv = input.replace(/÷/, "/");
        const replaceX = replaceDiv.replace(/x/, "*");
        setInput(`${eval(replaceX)}`);
        setNumberwang("THAT'S NUMBERWANG");
        break;

      case (buttonPushed=="."):
        const lastDecimalChar = lastArr.charAt(lastArr.length-1)
        if (lastDecimalChar == "." || lastArr.includes(".")) {
          setInput(`${input}`)
        } else {
          setInput(`${input}.`)
        }
        break;

      default:
        setInput(buttonPushed);
    }
  }
  
  const clearDisplay = () => {
      setInput("0");
      setNumberwang("");
  }
  
  // initializing to zero
  React.useEffect(() => {
    setInput("0");
  }, []);
  
  return <div className="container">
    <Readout content={input} opChain={numberwang}/>
    <div id="nums">
      {numbers.map((n,i)=> {
        if (i>0) {
          return <Tapper 
                   element={i} 
                   buttonID={n}
                   calculate={calculate}
                   />
        }
      })}
    </div>
    <div id="operations">
      {operations.map((n,i) => {
        return <Tapper 
                 element={n.operator} 
                 buttonID={n.id} 
                 calculate={calculate}
                 />
      })}
    </div>
    <Tapper element="clear"
            buttonID="clear"
            calculate={clearDisplay} />
    
    <Tapper element="0"
            buttonID="zero"
            calculate={calculate} />
    
    <Tapper element="."
            buttonID="decimal"
            calculate={calculate} />
    
    <Tapper element="="
            buttonID="equals"
            calculate={calculate} />
  </div>
}

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

Console