cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <div id="help" class="blur_layer">
  <div class="transparent_background"></div>
  <div class="modal_window">
  <p>This web calculator has been made by Edgar S. Hurtado as part of the freeCodeCamp challenges</p>
  <br>
  <p>Personal website: <a href="http://edgarsh.es" target="_blank">edgarsh.es</a></p>
  <p>Twitter: <a href="https://twitter.com/edgarshurtado" target="_blank">@edgarshurtado</a></p>
  </div>
</div>

<div id="calc_body">
  <img id="logo" src="https://raw.githubusercontent.com/FreeCodeCamp/FCCAssets/master/Logos%20and%20Covers/freeCodeCamp-onBrown-1200.png" alt="" />
  <div id="display">
    <p id="displayNumbers">
      <span id="sign"></span>
      <span id="number">0</span>
    </p>
  </div>

  <table id="buttons_pad">
    <tr>
      <td id="c" class="button dark_brown">C</td>
      <td id="del" class="button dark_brown" colspan="2">DEL</td>
      <td class="button operational_button dark_brown">^</td>
      <td id="help_button" class="button operational_button dark_brown">?</td>
    </tr>
    <tr>
      <td class="button numeric_button light_brown">7</td>
      <td class="button numeric_button light_brown">8</td>
      <td class="button numeric_button light_brown">9</td>
      <td class="button operational_button dark_brown">+</td>
      <td class="button operational_button dark_brown">%</td>
    </tr>
    <tr>
      <td class="button numeric_button light_brown">4</td>
      <td class="button numeric_button light_brown">5</td>
      <td class="button numeric_button light_brown">6</td>
      <td class="button operational_button dark_brown">-</td>
      <td class="button operational_button dark_brown">√</td>
    </tr>
    <tr>
      <td class="button numeric_button light_brown">1</td>
      <td class="button numeric_button light_brown">2</td>
      <td class="button numeric_button light_brown">3</td>
      <td class="button operational_button dark_brown">*</td>
      <td class="button operational_button dark_brown">/</td>
    </tr>
    <tr>
      <td id="change_sign" class="button light_brown">&plusmn;</td>
      <td class="button numeric_button light_brown">0</td>
      <td class="button numeric_button light_brown">.</td>
      <td id="equal_button" class="button dark_brown" colspan="2">=</td>
    </tr>
  </table>
</div>
            
          
!
            
              * {
    margin:0;
    padding:0;
}
#calc_body {
    position: fixed;
    top: 15%;
    left: 35%;
    background-color: #4a2b0f;
    height:490px;
    width: 445px;
}

#logo {
    height: 30px;
    margin-left: 8px;
}

#display {
    margin-left: 15px;
    width: 417px;
    height: 82px;
    background-color: rgb(155, 243, 255);
}

#display p {
    position: relative;
    top: 15px;
    right: 10px;
    font-size: 3em;
    color: rgb(51, 96, 102);
    text-align: right;
}

#buttons_pad {
    text-align: center;
    margin-left: 5px;
    border-spacing: 10px;
    color: white;
}

.button {
    width: 75px;
    height: 62px;
    font-size: 2em;

    -webkit-box-shadow: 0 8px 6px -6px black;
    -moz-box-shadow: 0 8px 6px -6px black;
    box-shadow: 0 8px 6px -6px black;
}

.button:active {
    -moz-box-shadow:    inset 3px 3px 10px black;
    -webkit-box-shadow: inset 3px 3px 10px black;
    box-shadow:         inset 3px 3px 10px black;
}

.light_brown {
    background-color: rgb(191, 111, 40);
}

.dark_brown {
    background-color: rgb(134, 78, 27);
}

.blur_layer{
    position:fixed;
    height:100%;
    width:100%;
    display: none;
    z-index: 3;
}

.transparent_background{
    height:100%;
    width:100%;
    background-color: rgba(0, 0, 0, 0.5);
}

.modal_window{
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 1;
    background-color: white;
    padding: 15px 5px 15px 5px;
    border-radius: 15px;
}

.modal_window p{
    margin: 5px 5px 5px 5px;
}

            
          
!
            
              //TODO: When the sign is changed the result is not displayed properly

/*jslint browser: true*/
/*global $, jQuery, alert*/

// --------------------
//      CALC LOGIC
// --------------------

var calcObject;
calcObject = {
  operand1: null,
  operand2: null,
  activeOperator: null,
  lastButton: "",

  // Sets the operand in the calc. If there's no activeOperator, the number
  // is sets to the operand1, if there is it sets to the operand2
  setOperand: function(number) {
    if (this.activeOperator === null) {
      this.operand1 = number;
    } else {
      this.operand2 = number;
    }
  },

  setActiveOperator: function(operatorSymbol) {
    this.activeOperator = operatorSymbol;
  },

  addition: function() {
    return this.operand1 + this.operand2;
  },

  percent: function() {
    return this.operand1 * this.operand2 / 100;
  },

  substraction: function() {
    return this.operand1 - this.operand2;
  },

  sqrt: function() {
    return Math.sqrt(this.operand1);
  },

  multiplication: function() {
    return this.operand1 * this.operand2;
  },

  division: function() {
    return this.operand1 / this.operand2;
  },

  pow: function() {
    return Math.pow(this.operand1, this.operand2);
  },

  solve: function() {
    var solution = this[this.activeOperator]();
    solution = parseFloat(parseFloat(solution).toPrecision(15));
    this.resetOperands();
    return solution;
  },

  resetOperands: function() {
    this.operand1 = null;
    this.operand2 = null;
    this.activeOperator = null;
  },

  operationReady: function() {
    if (this.activeOperator === "sqrt") {
      // The square root is special because it requires just 1 operand
      return this.operand1 !== null;
    }
    return this.operand1 !== null && this.operand2 !== null && this.activeOperator !== null;
  }
};

// ------------------------------
// GUI Logic
// ------------------------------

// -- Delete current number in display --
function deleteCurrentNumber() {
  $('#number').text('0');
  $('#sign').text('');
}

function printNumber(num) {
  var $numberHolder = $('#number');
  var $signHolder = $('#sign');

  if (num < 0) {
    $signHolder.text("-");
    num = num * (-1);
  } else {
    $signHolder.text("");
  }

  num = num.toString().slice(0, 15);

  $numberHolder.text(num);
}

// Parse the display number
function parseDisplayNumber() {
  var numberString = $('#sign').text() + $('#number').text();
  var parsedNumber;

  if (numberString.indexOf(".") !== -1) {
    parsedNumber = parseFloat(numberString);
  } else {
    parsedNumber = parseInt(numberString, 10);
  }
  return parsedNumber;
}

// Introducing numbers to the display control
function addNumberToDisplay(newNumber) {
  var $number = $('#number');
  var $sign = $('#sign');
  var currentNumber = $sign.text() + $number.text();
  if (currentNumber === '0' || calcObject.lastButton.indexOf('operational_button') !== -1 || calcObject.lastButton === "equal_button") {
    if (calcObject.lastButton === "change_sign") {
      // In case the user hits the change_sign button just right after
      // hitting an operational button because he wants a negative number
      // as second operand.
      printNumber(newNumber);
    } else if (newNumber === '.') {
      printNumber("0.");
    } else {
      deleteCurrentNumber();
      printNumber(newNumber);
    }
  } else if (currentNumber.length < 15) {
    printNumber(currentNumber + newNumber);
  }
}

// Checks if the operation is ready and then solves it
function solveCurrentOperation() {
  if (calcObject.operationReady()) {
    var solution = calcObject.solve();
    calcObject.resetOperands();
    printNumber(solution);
  }
}

// ------------------------------
// BUTTONS EVENTS
// ------------------------------

// Introduce numbers by clicking
$(".numeric_button").click(function() {
  var $this = $(this);
  if ($this.text() !== '.' || $('#number').text().indexOf('.') === -1) {
    addNumberToDisplay($this.text());
  }
  calcObject.lastButton = $this.attr('class');
});

// -- Delete current number in display --
$('#c').click(deleteCurrentNumber);

// -- Reset the calculator --
$('#del').click(function() {
  calcObject.resetOperands();
  deleteCurrentNumber();
});

// +- button
$("#change_sign").click(function() {
  var $sign = $('#sign');
  var currentSign = $sign.text();

  if (calcObject.lastButton.indexOf("operational_button") !== -1 && calcObject.operand2 === null) {
    $sign.text("-");
    $('#number').text("0");
    calcObject.setOperand(parseDisplayNumber);
  } else {
    if (currentSign === "") {
      $sign.text("-");
    } else if (currentSign === "-") {
      $sign.text("");
    }
  }

  calcObject.lastButton = $(this).attr('id');
});

// Operational_buttons
$('.operational_button').click(function() {
  calcObject.setOperand(parseDisplayNumber());
  if (calcObject.operationReady()) {
    solveCurrentOperation();
    // In case we are concatenating operations, to set the result number as
    // operand1
    calcObject.setOperand(parseDisplayNumber());
  }

  var operationSymbol = $(this).text();

  switch (operationSymbol) {
    case '+':
      calcObject.setActiveOperator('addition');
      break;
    case '%':
      calcObject.setActiveOperator('percent');
      break;
    case '-':
      calcObject.setActiveOperator('substraction');
      break;
    case '√':
      calcObject.setActiveOperator('sqrt');
      solveCurrentOperation();
      break;
    case '*':
      calcObject.setActiveOperator('multiplication');
      break;
    case '/':
      calcObject.setActiveOperator('division');
      break;
    case '^':
      calcObject.setActiveOperator('pow');
      break;
    default:

  }
  calcObject.lastButton = $(this).attr('class');
});

$('#equal_button').click(function() {
  calcObject.setOperand(parseDisplayNumber());

  if (calcObject.operationReady()) {
    solveCurrentOperation();
  }

  calcObject.resetOperands();
  calcObject.lastButton = $(this).attr('id');
});

$('#help_button').click(function() {
  $('#help').show();
});

$('.transparent_background').click(function() {
  $('#help').hide();
});
            
          
!
999px
Loading ..................

Console