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="header">
</div>
<div id="mainContainer">
  <div id="computerName">
    <h5>Does !compute for Robots™</h5>
  </div>
  <div id="results">
    <span id="displayNum"></span>
    <span id="displayLastCalc"></span>
    <span id="displayError"></span>
  </div>
  <div id="inputKeysRow">
    <div class="aCColumn">
      <div class="keys" id="keyAC">
        <span>AC</span>
      </div>
      <div class="keys" id="keyMPlus">
        <span>M+</span>
      </div>
      <div class="keys" id="keyMR">
        <span>MR</span>
      </div>
      <div class="keys" id="keyMC">
        <span>MC</span>
      </div>
    </div>
    <div class="cEColumn">
      <div class="keys" id="keyCE">
        <span>CE</span>
      </div>
      <div class="keys" id="key7">
        <span>7</span>
      </div>
      <div class="keys" id="key4">
        <span>4</span>
      </div>
      <div class="keys" id="key1">
        <span>1</span>
      </div>
    </div>
    <div class="divideColumn">
      <div class="keys" id="keyDivide">
        <span>/</span>
      </div>
      <div class="keys" id="key8">
        <span>8</span>
      </div>
      <div class="keys" id="key5">
        <span>5</span>
      </div>
      <div class="keys" id="key2">
        <span id="key2s">2</span>
      </div>
      <div class="keys" id="key0">
        <span>0</span>
      </div>
    </div>
    <div class="multiplyColumn">
      <div class="keys" id="keyMultiply">
        <span>*</span>
      </div>
      <div class="keys" id="key9">
        <span>9</span>
      </div>
      <div class="keys" id="key6">
        <span>6</span>
      </div>
      <div class="keys" id="key3">
        <span>3</span>
      </div>
      <div class="keys" id="keyDecimal">
        <span>.</span>
      </div>
    </div>
    <div class="subtractColumn">
      <div class="keys" id="keySubtract">
        <span>-</span>
      </div>
      <div class="keys" id="keyPlus">
        <span>+</span>
      </div>
      <div class="keys" id="keyEnter">
        <span>E</span>
      </div>
    </div>
  </div>
</div>
<div id="footer">
  <h3>An app by Tom Corbin</h3>
</div>
              
            
!

CSS

              
                /* fonts */
@import url("https://fonts.googleapis.com/css?family=Poppins:400,700|Rajdhani:400,700");

/* body and main structure */
body {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  min-height: 100vh;
  min-width: 100vw;
  margin: 50px 0;
  background: #f3f2b9;
  font-family: "Poppins", sans-serif;
  font-size: 10px;
}

#mainContainer {
  display: flex;
  flex-direction: column;
  color: black;
  font-size: 2em;
  background: #e4f0fc;
  width: 300px;
  padding: 0 5px;
  border: 3px solid #9099a2;
  box-shadow: 3px 6px #448599;
  border-radius: 25px;
  margin: 20px 0 0 0;
}

/* advert */
#computerName {
  align-self: center;
  color: #7d853c;
  font-size: 1.4em;
  font-weight: 700;
  font-family: "Rajdhani", sans-serif;
  border-radius: 10px;
  margin: 5px 0;
}

#computerName > h5 {
  margin: 0;
  text-shadow: 0px 1px #f3f2b9;
}

/* display */
#results {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  text-align: right;
  line-height: 1.2em;
  background: #d9d9d9;
  height: 80px;
  border: 2px solid #9099a2;
  box-shadow: -2px -2px 1px 1px #448599;
  border-radius: 25px;
  margin: 5px 0;
}

#displayNum {
  color: #76323f;
  text-shadow: 1px 2px #bfd8d2;
  font-size: 1.5em;
  padding: 2px 10px 0 10px;
}

#displayLastCalc {
  color: #7a4d55;
  font-size: 0.9em;
  padding: 0 13px 0 5px;
  margin: 1px 0;
  white-space: nowrap;
  overflow: hidden;
}

#displayError {
  color: #7a4d55;
  font-size: 0.6em;
  padding: 0 13px 0px 5px;
  margin: 1px 0 5px 0;
}

/* keyboard */
#inputKeysRow {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  padding: 0 0 5px 0;
  border-top: 2px solid #448599;
}

.aCColumn,
.subtractColumn {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.cEColumn {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  background: #448599;
}

.divideColumn {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.multiplyColumn {
  display: flex;
  flex-direction: column;
  justify-content: center;
  background: #448599;
}

.cEColumn > .keys,
.multiplyColumn > .keys {
  box-shadow: 1px 2px #bfd8d2;
}

/* keys */
.keys {
  color: #721a2a;
  text-shadow: 1px 1px #7d853c;
  background: #bfd8d2;
  width: 30px;
  height: auto;
  padding: 10px;
  border: 1px solid #565656;
  border-radius: 25px;
  box-shadow: 1px 2px #448599;
  margin: 5px;
  cursor: pointer;
  transition: all 0.15s ease;
}

#keyAC,
#keyCE {
  background: #c4608e;
}

#keyMPlus,
#keyMR,
#keyMC {
  background: #cf5260;
}

#keyPercent,
#keyDivide,
#keyMultiply,
#keySubtract,
#keyPlus {
  background: #deb13a;
}

#keyPlus {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 92px;
}

#keyEnter {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 92px;
  background: #de744c;
}

/* LoVe Fears HAte*/
.keys:hover {
  color: #de744c;
}

#keyEnter:hover {
  color: #deb13a;
}

.keys:active {
  background: #7d853c;
}

#keyAC:active,
#keyMPlus:active,
#keyMR:active,
#keyMC:active,
#keyCE:active {
  background: #deb13a;
}

#keyDivide:active,
#keyMultiply:active,
#keySubtract:active,
#keyPlus:active {
  color: #deb13a;
  background: #de744c;
}

#keyEnter:active {
  color: #de744c;
  background: #deb13a;
}

/* footer */
#footer {
  color: #deb13a;
  text-shadow: 0px 1px #448599;
  /* text-shadow: 0px 1px #7d853c; */
  font-size: 1.5em;
}

              
            
!

JS

              
                "use strict";

//variables
let statusDisplay = true;
let newNum = Number;
let newDigit = "";
let currentOperator = "";
let holdDigits = [];
let numsArr = [];
let arithOper = null;
let runningResult = null;
let displayRunningResult = null;
let result = null;
let displayResult = null;
let memory = "";
let displayMemory = "";
const displayNum = document.querySelector("#displayNum");
const displayLastCalc = document.querySelector("#displayLastCalc");
const displayError = document.querySelector("#displayError");
let thoughtForRobot = "";
const thoughts = {
  0: "Out for a duck, robot",
  1: "...is the loneliest number that you'll ever do",
  2: "It's the loneliest number since the number one",
  3: "...to get ready, now go, robot, go",
  "3.14": "We like pi, robot",
  4: "Watch out for a gang of this, robot",
  5: "There are only this many Platonic solids, robot",
  6: "...of one, half a dozen of the other, robot",
  7: "Luck be a lady, robot",
  8: "Useless without a cox, robot",
  9: "A stitch in time, robot",
  10: "Humans have this many fingers, robot",
  11: "S. Tendulkar belongs in any best one, robot",
  12: "...a superior highly composite number, robot",
  13: "A baker's dozen is prime, robot",
  14: "...est quatorze en français, robot",
  15: "...on a rugby side, robot",
  16: "You're this, you're beautiful, and you're a robot",
  17: "The sum of the first four primes, Android",
  18: "This group's members are noble, robot",
  19: "Prime reason we can't dance together, robot",
  20: "Is it bigger than a bread box, robot?",
  21: "Pips on dice, robot",
  22: "Lazy caterer's sequence turns 6 into this, robot",
  23: "This lane is reserved for The Dude, robot",
  24: "...major and minor keys, robot",
  25: "...quid is a pony, robot",
  30: "Atomic number of Zinc (Zn), robot",
  40: "℃ x 9 / 5 + 32 = 104 ℉, robot",
  42: "Life, the Universe and Everything, robot",
  45: "Come on robot, let the good times roll",
  47: "Prime and ready for something else, robot",
  50: "My kingdom for a chanceless 50, robot",
  60: "...% of the time, it works every time, robot",
  66: "Get your kicks on this route, robot",
  69: "An odious rather than evil number, robot", // 1000101
  70: "...is the new fifty, robot",
  78: "Come on robot, let the good times roll",
  80: "P. Fogg wagered he'd need this many, robot",
  99: "Luftballons, robot",
  100: "A ton, robot",
  111: "Raise your leg! We're on a Nelson, robot",
  147: "Man, robot",
  161: "Dude, robot",
  200: "A double century, robot",
  300: "A triple century, robot",
  451: "Books are for reading, not burning, robot",
  500: "I would walk this far to haver to you, robot",
  523: "Jimmy's test wickets, last I checked, robot",
  568: "I'll see you later, robot",
  666: "An odious rather than evil number, robot", // 1010011010
  1964: "Rock ‘Em Sock ‘Em Robots",
  1965: "Danger, Will Robinson!",
  1966: "Pet Sounds, robot",
  1967: "Sgt. Pepper's Lonely Hearts Club Band, robot",
  1969: "Let It Bleed, robot",
  1971: "Honky Dory, robot",
  1983: "Dōmo arigatō, Mr. Roboto",
  1992: "It's a Shame About Ray, robot",
  1999: "So tonight I'm gonna party like it's..., robot",
  2000: "...party over, oops out of time, robot",
  Infinity: "...and beyond, robot!", // 1/0
  NaN: "Does !compute, robot!" // 0/0
};

//digits
document.querySelector("#key0").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key1").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key2").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key3").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key4").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key5").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key6").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key7").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key8").addEventListener("click", function() {
  enterNumber(this.textContent);
});
document.querySelector("#key9").addEventListener("click", function() {
  enterNumber(this.textContent);
});

//decimal
document.querySelector("#keyDecimal").addEventListener("click", function() {
  enterDecimal();
});

//operators
document.querySelector("#keyDivide").addEventListener("click", enterOperator);
document.querySelector("#keyMultiply").addEventListener("click", enterOperator);
document.querySelector("#keySubtract").addEventListener("click", enterOperator);
document.querySelector("#keyPlus").addEventListener("click", enterOperator);

//compute
document.querySelector("#keyEnter").addEventListener("click", compute);

//clear and memory
document.querySelector("#keyAC").addEventListener("click", clearData);
document.querySelector("#keyCE").addEventListener("click", clearLastInput);
document.querySelector("#keyMPlus").addEventListener("click", memoryPlus);
document.querySelector("#keyMR").addEventListener("click", memoryRecall);
document.querySelector("#keyMC").addEventListener("click", memoryClear);

//Does !compute for Robots functions

//Status of key variables:
//      result && displayResult should be set to same null status
//      runningResult && runningDisplayResult should be set to same null status
//      result set = to null during clearData(), updateCalcs(), updateDisplay()
//      result is !== null (set = to runningResult) during compute() (unless too many digits resets)
//      result is !== null (set = to runningResult) during calculateRunningResult() (only in }else{ case)
//      runningResult is set = to null during readyCalculator, triggered by clearData() and compute()
//      statusDisplay set = to true during clearData(), enterNumber(), enterDecimal()
//      statusDisplay set = to false during updateDisplay(), triggered by enterOperator()
//      statusDisplay set = to false during readyCalculator(), triggered by clearData() and compute()

//Note on rounding: carry through unrounded numbers for more accurate calculations:
//      displayResult (only rounded) from result during compute()
//      displayRunningResult (only rounded) from runningResult during storeNumbers()

function enterNumber(num) {
  clearDisplayError();
  if (displayNum.textContent.length < 13) {
    //Limits displayNum to 12 characters
    result === null ? (newDigit = "" + Number(num)) : updateCalcs(num);
    holdDigits.push(newDigit);
    //result && displayResult are always null at this point
    if (statusDisplay === true && displayNum.textContent === "0") {
      displayNum.textContent = newDigit;
    } else if (statusDisplay === true) {
      displayNum.textContent += newDigit;
    } else if (
      statusDisplay === false &&
      (displayNum.textContent === "0." || displayNum.textContent === ".")
    ) {
      displayNum.textContent += newDigit;
    } else {
      displayNum.textContent = newDigit;
    }
    statusDisplay = true;
  } else {
    //Resets calculator-- !robots would never need that many digits
    clearData();
    displayError.textContent = "!compute. Only robots need that many digits.";
  }
}

function enterDecimal() {
  clearDisplayError();
  //Check if user is entering multiple decimals
  if (displayNum.textContent.match(/\./) && statusDisplay === true) {
    displayError.textContent = "Decimal already entered, robot.";
    //If not, enter decimal
  } else {
    if (displayNum.textContent === "0") {
      newDigit = "0.";
    } else if (result === null && statusDisplay === true) {
      newDigit = ".";
    } else if (result !== null && statusDisplay === true) {
      newDigit = ".";
    } else if (result !== null && statusDisplay === false) {
      newDigit = "0.";
    } else {
      newDigit = "0.";
    }
    holdDigits.push(newDigit);
    if (statusDisplay === true && displayNum.textContent === "0") {
      displayNum.textContent = newDigit;
    } else if (statusDisplay === true && displayNum.textContent === "0.") {
      displayNum.textContent += newDigit;
    } else if (statusDisplay === true) {
      displayNum.textContent += newDigit;
    } else {
      displayNum.textContent = newDigit;
    }
    statusDisplay = true;
  }
}

function enterOperator() {
  //this === "#keyDivide" || "#keyMultiply" || "#keyMinus" || "#keyPlus"
  currentOperator = this.textContent;
  if (result !== null) {
    runningResult = result;
    displayLastCalc.textContent = "";
  }
  storeNumbers(currentOperator);
  if (displayLastCalc.textContent === "") {
    displayLastCalc.textContent =
      displayNum.textContent + ` ${currentOperator}`;
  } else {
    displayLastCalc.textContent +=
      displayNum.textContent + ` ${currentOperator}`;
  }
  updateDisplay();
  changeOper(currentOperator);
}

function updateCalcs(num) {
  displayLastCalc.textContent = "";
  newDigit = "" + Number(num);
  result = null;
  displayResult = null;
}

function updateDisplay() {
  displayNum.textContent = displayRunningResult;
  holdDigits = [];
  statusDisplay = false;
  result = null;
  displayResult = null;
}

function changeOper(operator) {
  arithOper = operator;
}

function deepThought() {
  thoughtForRobot = "";
  if (thoughts[displayNum.textContent] !== undefined) {
    thoughtForRobot = thoughts[displayNum.textContent];
  }
}

function memoryPlus() {
  if (displayNum.textContent.includes(".") && result !== null) {
    memory = result;
  } else if (displayNum.textContent == displayMemory) {
    // Preserve unrounded numbers for more accurate calculations
    //Comparing string == number, thus need coercion
    console.log("Will not overwrite memory with displayMemory, robot");
  } else {
    memory = displayNum.textContent;
  }
  displayError.textContent = `Memory = ${memory}`;
}

function memoryRecall() {
  //Round: see Note on rounding above
  displayMemory = Math.round(memory * 1000000) / 1000000;
  enterNumber(displayMemory);
  displayError.textContent = `Memory = ${memory}`;
}

function memoryClear() {
  memory = "";
  displayMemory = "";
}

function storeNumbers(operator) {
  if (result !== null) {
    newNum = result;
    numsArr.push(newNum);
  } else {
    newNum = Number(holdDigits.join(""));
    numsArr.push(newNum);
    //Round: set runningResult = newNum -- then round runningResult to get displayRunningResult
    if (runningResult === null) {
      runningResult = newNum;
      displayRunningResult = Math.round(runningResult * 1000) / 1000;
    } else {
      calculateRunningResult();
    }
  }
  changeOper(operator);
}

function compute() {
  //Check if user is a robot
  const userIsARobot = false;
  if (userIsARobot) return;
  
  //Check if user has already computed
  if (displayLastCalc.textContent.match(/=/)) {
    displayError.textContent = "Already computed, robot.";
    //If not, compute
  } else {
    storeNumbers();
    displayLastCalc.textContent === ""
      ? (displayLastCalc.textContent = displayNum.textContent + " = ")
      : (displayLastCalc.textContent += displayNum.textContent + " = ");
    //Round: set result = runningResult -- then round result to get displayResult
    result = runningResult;
    displayResult = Math.round(result * 1000) / 1000;
    displayNum.textContent = displayResult;
    if (displayNum.textContent.length < 13) {
      //Limits displayNum to 12 characters
      deepThought();
      readyCalculator();
      displayError.textContent = thoughtForRobot;
    } else {
      //Resets calculator-- !robots would never need that many digits
      clearData();
      displayError.textContent = "!compute. Only robots need that many digits.";
    }
  }
}

function calculateRunningResult() {
  if (arithOper.includes("+")) {
    runningResult = runningResult + newNum;
  } else if (arithOper.includes("-")) {
    runningResult = runningResult - newNum;
  } else if (arithOper.includes("*")) {
    runningResult = runningResult * newNum;
  } else if (arithOper.includes("/")) {
    runningResult = runningResult / newNum;
  } else {
    result = runningResult;
  }
  //Round runningResult to get displayRunningResult
  displayRunningResult = Math.round(runningResult * 1000) / 1000;
}

function clearDisplayError() {
  displayError.textContent = "";
}

function readyCalculator() {
  clearDisplayError();
  statusDisplay = false;
  newNum = Number;
  newDigit = "";
  holdDigits = [];
  numsArr = [];
  arithOper = null;
  runningResult = null;
  displayRunningResult = null;
  //result is NOT set to null --> enables storeNumbers(), enterOperator(), and memoryPlus()
  //...NEITHER is displayResult set to null -- to keep these variables in lockstep
}

function clearLastInput() {
  if (holdDigits.length !== 0) {
    holdDigits.pop();
    displayNum.textContent = displayNum.textContent.slice(0, -1);
    clearDisplayError();
  } else {
    displayNum.textContent = "0";
    displayError.textContent = "Hey, robot, no more buwwets!";
  }
}

function clearData() {
  readyCalculator();
  memoryClear();
  statusDisplay = true;
  result = null;
  displayResult = null;
  displayNum.textContent = "0";
  displayLastCalc.textContent = "";
}

window.addEventListener("pageshow", clearData);

              
            
!
999px

Console