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 class="wrapp">
  
  <div class="social">
    <h2>Look more:</h2>
    <div class="social_list">
      <ul>
        <li class="github"><a href="https://github.com/halyna-dev?tab=repositories" target="_blank"><i class="fa fa-github" aria-hidden="true"></i></a></li>
        <li class="linkedin"><a href="https://www.linkedin.com/in/halyna-taran-22b833148/" target="_blank"><i class="fa fa-linkedin-square" aria-hidden="true"></i></a></li>
        <li class="codepen"><a href="https://codepen.io/halyna-dev/" target="_blank"><i class="fa fa-codepen" aria-hidden="true"></i></a></li>
      </ul>
    </div>
  </div>
  
  <div class="bg-block">
    <div class="bg-before">
    </div>
    <div class="bg">
    </div>
    <div class="bg-after">
    </div>
  </div>
  
  <div class="calculator">
    
    <div class="header">
      <div class="screen">
        <div class="score">0</div>
        <div class="operation">0</div>
      </div>
    </div>
    
    <div class="body">
      <button class="button">AC</button>
      <button class="button">7</button>
      <button class="button">4</button>
      <button class="button">1</button>
      <button class="button">0</button>
      <button class="button">CE</button>
      <button class="button">8</button>
      <button class="button">5</button>
      <button class="button">2</button>
      <button class="button">.</button>
      <button class="button">%</button>
      <button class="button">9</button>
      <button class="button">6</button>
      <button class="button">3</button>
      <button class="button">=</button>
      <button class="button">/</button>
      <button class="button">*</button>
      <button class="button">-</button>
      <button class="button button-long">+</button>
    </div>
    
  </div>
  
  
  
</div>
            
          
!
            
              body{margin:0;}
.wrapp{
  wigth: 100vw;
  height: 100vh;
  min-height: 500px;
  position: relative;
  overflow: hidden;
  
  font-family: Inconsolata;
}

/* create background group */
.bg-block{
  width: 100vw;
  height: 100vh;
}
.bg {
  background: url('https://thumbs.dreamstime.com/b/seamless-background-christmas-trees-snow-horizontal-holiday-against-blue-sky-vector-44970300.jpg');
  background-size: cover;
  background-position: center;
  position: relative;
  left: 0;
  z-index: -10;
  width: inherit;
  height: 100vh;
  min-height: 500px;
  overflow: hidden;
  animation: move-clouds-one 20s linear 0s;
}
.bg-after {
  content: '';
  background: url('https://thumbs.dreamstime.com/b/seamless-background-christmas-trees-snow-horizontal-holiday-against-blue-sky-vector-44970300.jpg');
  background-size: cover;
  background-position: center;
  position: absolute;
  top: 0;
  left: 100vw;
  z-index: -9;
  width: inherit;
  height: 100vh;
  min-height: 500px;
  overflow: hidden;
  animation: move-clouds 40s linear 0s infinite;
}
.bg-before {
  content: '';
  background: url('https://thumbs.dreamstime.com/b/seamless-background-christmas-trees-snow-horizontal-holiday-against-blue-sky-vector-44970300.jpg');
  background-size: cover;
  background-position: center;
  position: absolute;
  top: 0;
  left: -100vw;
  z-index: -9;
  width: inherit;
  height: 100vh;
  min-height: 500px;
  overflow: hidden;
  animation: move-clouds 40s linear 20s infinite;
}
/* animate background */
@keyframes move-clouds-one {
 from { left: 0; }
 to { left: -100vw; }
}
@keyframes move-clouds {
 from { left: 100vw; }
 to { left: -100vw; }
}

/* style from calculator */

.calculator{
  position: absolute;
  top: calc(50vh - 200px);
  left: calc(50% - 150px);
  width: 300px;
  height: 400px;
  background: rgba(255,255,255, .5);
  border: 1px solid #FFF;
  border-radius: 5px;
  overflow: hidden;
}

.header{
  padding: 10px;
}
.screen{
  border: 1px solid #9bd0ef;
  border-radius: 5px;
  overflow: hidden;
  color: #456577;
}
.score{
  background: rgba(82, 120, 143, .2);
  padding: 10px 5px;
  text-align: right;
}
.operation{
  background: rgba(82, 120, 143, .2);
  padding: 5px;
  border-top: 1px solid #9bd0ef;
  text-align: right;
}

.body{
  width: 300px;
  height: 300px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}
.button {
  width: 50px;
  height: 50px;
  margin: 5px;
  border: 1px solid #9bd0ef;
  border-radius: 5px;
  cursor: pointer;
  font-size: 18px;
  font-family: monospace;
  color: #456577;
}
.button-long{
  height: 110px;
}
.button:hover{
  border: 1px solid #FFF;
}


.social{
  z-index: 1000;
  position: fixed;
  top: 10px;
  left: calc(100vw - 150px);
  width: 140px;
  background: rgba(255,255,255, .5);
  color: #456577;
  border-radius: 5px;
  border: 1px solid transparent;
}
.social h2{
  text-transform: uppercase;
  font-size: 16px;
  font-family: Verdana;
  padding: 10px;
}
.social_list ul {
  // text-align: center;
  margin-left: -40px;
  li {
    list-style: none;
    display: inline-block;
  }
  a {
    color: #456577;
    font-size: 24px;
    padding: 0 10px;
  }
}
            
          
!
            
              // DOM Elements
const score = document.querySelector('.score');
const operation = document.querySelector('.operation');
const body = document.querySelector('.body');

const calculator = {
  value: [], //current screen value
  cacheOperation: [], //array of cache numbers and operations
  
  updateScreenVal: function(nextNum) {
    console.log('start updateScreenVal');
    if ( nextNum === '.' ) {
      if (this.value[0] == undefined) { this.value[0] = '0'; };
      if ( !this.value.includes('.') ) { this.value.push(nextNum); };
    }
    else if (nextNum.search(/[0-9]/) ==  0){ //becouse Listener can return all form, if click was be out of buttons
      this.value.push(nextNum);
    };

    if ((this.value[0] === '0') && ((this.value[1]).search(/[0-9]/) !==  -1) ) {
      this.value.splice(0,1);
    };
    
    console.log('this.value = ', this.value);
    score.innerHTML = this.value.join('');
    return;
  },
  
  updateCacheOperation: function(operator) {
    console.log('Start updateOperation');
    operation.innerHTML = '';
    let val = this.value.join('');
    if (val === '0.') {val = '0';}
    if (val === '') {val = '0';}
    
    score.innerHTML = 0;
    this.value = [];
    
    this.cacheOperation.push(val);
    this.cacheOperation.push(operator);
    
    console.log('cacheOperation: ', this.cacheOperation);
    operation.innerHTML = this.cacheOperation.join('');
    return;
    },
  
  backspace: function() {
    this.value.splice([this.value.length-1],1);
    if (this.value.length === 0) this.value[0] = '0';
    score.innerHTML = this.value.join('');
  },
  
  clean: function() {
    score.innerHTML = 0;
    operation.innerHTML = 0;
    this.value = [];
    this.cacheOperation = [];
  },
  
  equals: function() { 
    let val = this.value.join('');
    if (val === '0.') {val = '0';};  
    if (val === '') {val = '0';}
    this.cacheOperation.push(val);
    
    //help fix binar bug in float numbers
    function getDecimalLength(num){
      var str = num + '';
      var zeroPos = str.indexOf(".");
      if (zeroPos == -1) return 0;
      str = str.slice(zeroPos);
      return str.length - 1;
    };
    
    //Prioritate operation
    while( this.cacheOperation.includes('*') ||  this.cacheOperation.includes('/') ){
      let x = this.cacheOperation.indexOf('*');
      let y = this.cacheOperation.indexOf('/');
      let z;
           
      if ( (x < y) && (x != -1) && (y != -1)){
        let multiDec = getDecimalLength(this.cacheOperation[x - 1]) + getDecimalLength(this.cacheOperation[x + 1]);
        z = this.cacheOperation[x - 1] * this.cacheOperation[x + 1];
        z = z.toFixed(multiDec);
        this.cacheOperation.splice(x - 1, 3, z);
      }
      if ( (x > y) && (x != -1) && (y != -1)){
        let divDec = getDecimalLength(this.cacheOperation[y - 1]) - getDecimalLength(this.cacheOperation[y + 1]);
        if (divDec < 0) divDec = 0;
        z = this.cacheOperation[y - 1] / this.cacheOperation[y + 1];
        z = z.toFixed(divDec);
        this.cacheOperation.splice(y - 1, 3, z);
      }
      if ( (x != -1) && (y === -1)){
        let multiDec = getDecimalLength(this.cacheOperation[x - 1]) + getDecimalLength(this.cacheOperation[x + 1]);
        z = this.cacheOperation[x - 1] * this.cacheOperation[x + 1];
        z = z.toFixed(multiDec);
        this.cacheOperation.splice(x - 1, 3, z);
      }
      if ( (x === -1) && (y != -1)){
        let divDec = getDecimalLength(this.cacheOperation[y - 1]) - getDecimalLength(this.cacheOperation[y + 1]);
        if (divDec < 0) divDec = 0;
        z = this.cacheOperation[y - 1] / this.cacheOperation[y + 1];
        z = z.toFixed(divDec);
        this.cacheOperation.splice(y - 1, 3, z);
      }
    }
    
    console.log('Prioritate operation complete, casheOperation= ', this.cacheOperation);
    
    //Start accumulate answere  
    let ans = this.cacheOperation[0] - 0;
    
    for (let i = 1; i < this.cacheOperation.length; i += 2) {
      ans = ans - 0;
      //check max decimal Length in float numbers
      let max = Math.max(getDecimalLength(ans), getDecimalLength(this.cacheOperation[i+1]));
      
      if (this.cacheOperation[i] == "+"){
        ans += this.cacheOperation[i + 1] - 0 ;
      }
      if (this.cacheOperation[i] == "-"){
        ans -= this.cacheOperation[i + 1] - 0 ;
      }
      if (this.cacheOperation[i] == "%"){
        ans %= this.cacheOperation[i + 1] - 0 ;
      }
      
      ans = ans.toFixed(max);
    };
    
    console.log('ans = ', ans);
    
    score.innerHTML = ans;
    operation.innerHTML = 0;
    this.value = [ans];
    this.cacheOperation = [];
  }
};

function control(a){
  switch(a){
        case "+":
        calculator.updateCacheOperation('+');
        break;
        case "-":
        calculator.updateCacheOperation('-');
        break;
        case "*":
        calculator.updateCacheOperation('*');
        break;
        case "/":
        calculator.updateCacheOperation('/');
        break;
        case "%":
        calculator.updateCacheOperation('%');
        break;
        case "AC":
        calculator.clean();
        break;
        case "CE":
        calculator.backspace();
        break;
        case "=":
        calculator.equals();
        break;
        default:
        calculator.updateScreenVal(a);
    }
}

body.addEventListener('click', function(e){  
  let val = e.target.innerHTML;
  control(val);
});
            
          
!
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