css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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="center">
      <div class="calculator" id="calc">
         <div class="top-bar"><span class="clear">C</span>
         <div class="screen"></div>
         </div>
         <div class="keys">
         <div class="row">
              <span>7</span>
              <span>8</span>
              <span>9</span>
              <span class="operator">+</span>
         </div>
         <div class="row">
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span class="operator">-</span>
          </div>
         <div class="row">
            <span>1</span>
            <span>2</span>
            <span>3</span>
            <span class="operator">/</span>
          </div>
          <div class="row">
               <span>0</span>
               <span>.</span>
               <span class="equals">=</span>
               <span class="operator">*</span>
             </div>
      </div>
   </div>
</div>
            
          
!
            
              $bg: #829fc2;
$button: #0b5ca8;
$eval: #30c487;
$op: #ffd166;
$calculator: #c30000;
$radius: 10px;
$display: #708090;
$font: 'Roboto Condensed';
$font-color: #fcfcfc;
$ht: 52px;
$lineht: 55px;

@mixin shadow($color) {
  box-shadow: 0 4px 1px darken($color, 15%);
}

/* Basic reset */
* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}

body, html {
  height: 100%;
  background-color: $bg;
  font-family: $font;
  font-size: 24px;
}

.center {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.calculator {
  background-color: $calculator;
  border-radius: $radius;
  box-shadow: 0 8px darken($calculator, 10%);
  height: 390px;
  min-width: 390px;
  width: 390px;
}

.top-bar {
  display: flex;
  width: 100%;

  .clear {
    @include shadow(darken($op, 15%));
    background: $op;
    border-radius: $radius;
    color: darken($font-color, 50%);
    flex: .9;
    font-size: 22px;
    margin: 10px;
    height: $ht;
    line-height: $lineht;
    text-align: center;
    &:hover {
      color: yellow;
      cursor: pointer;
    }
  }

  .screen {
    background: $display;
    border-radius: $radius;
    box-shadow: inset 0 4px 5px darken($calculator, 30%);
    color: $font-color;
    flex: 3;
    font-size: 28px;
    height: $ht;
    margin: 10px;
    padding: 25px 30px;
    text-align: right;
    line-height: 12px;
  }
}

.row {display: flex;}

.keys {
  display: flex;
  flex-flow: column wrap;

  span {
    @include shadow($button);
    background: $button;
    border-radius: $radius;
    height: $ht;
    line-height: $lineht;
    margin: 11px;
    width: 75px;
    text-align: center;
    color: $font-color;

    &.operator {
      @include shadow(darken($op, 15%));
      background: $op;
      color: darken($font-color, 50%);
    }

    &.equals {
      @include shadow($eval);
      background: $eval;
      color: darken($font-color, 50%);
    }
    &:hover {
      color: yellow;
      cursor: pointer;
    }
  }
}

            
          
!
            
              const myButtons = document.querySelectorAll('span');
//const butt = document.querySelector(".calculator");

const screen = document.querySelector(".screen");
let entries = [];
let result = "";
let op = "";

screen.innerHTML = 0;

//const el = document.getElementById("calc");

// el.addEventListener("mouseover", function(e){
//     console.log('wat', e);
//     //alert('mousey');
// });


for (let i = 0; i < myButtons.length; i++) {
    //console.log(myButtons[i].innerHTML);
 
    myButtons[i].addEventListener("click", function(){;
        //console.log(myButtons[i].innerHTML);
       // console.log(myButtons[i].className);
       if(myButtons[i].className === "clear"){
            screen.innerHTML = 0;
            //reset global variables
            reset();
         }
        else { 
        if((myButtons[i].className != "operator") && (myButtons[i].className != "equals")){
            //console.log('num is ',  typeof(num));
            entries.push(myButtons[i].innerHTML);
            screen.innerHTML = doScr(entries);
 
        }

        if(myButtons[i].className === "operator"){
            op = getOperator(myButtons[i].innerHTML);
            entries.push(myButtons[i].innerHTML);
            }

        if(myButtons[i].className === "equals"){
            console.log("entries is", entries);
            if(result != "")
               { 
                console.log("current result is ", result);
                console.log("op is", op);
                //do operation on the entries after the op to get other num
                result = doOp(entries);
                console.log("new result is", result);
                }
           else{result = doOperation(entries);}
            console.log(result);
 
                if(isFinite(result)){
                    // console.log("num is", num);  
                    // console.log("result is", result);  
                     console.log("result type is", typeof(result));  
                  screen.innerHTML = result.toFixed(2).replace(/\.?0*$/g,'');
                 }
                else {
                    screen.innerHTML = "N/A"; //no dividing by zero
                    //reset result if divided by zero
                    //reset global variables
                        reset();
                    }
                }    
                // op = "";
                
        }    
    }, false);
  }


function getOperator(myOp){
    console.log(myOp);
    let theOp = "";
    switch(myOp){
        case '+':
            theOp = myOp;
            break;
        case '-':
            theOp = myOp;
            break;
        case '/':
            theOp = myOp;
            break;
        case '*':
            theOp = myOp;
            break;
        default:
            break;    
    }
    return theOp;
}

function doOp(arr) {
    let myIndex = arr.lastIndexOf(op);
    let list = arr.slice(myIndex, arr.length);
    //old result needs to go on beginning of array
    list.unshift(result);
    console.log ("list is", list);
    //sometimes it has more than 3 items in the list
    //let answer = calculate(list[0], list[2], list[1]);
    let answer = doOperation(list);
    return answer;
}

function doOperation(arr){
     let n1 = "", n2 = "";

     let opReached = false;
    arr.map(function(item){
        if((item != op) && (opReached === false))
           {n1 = n1 + item;}
         else {
             if(item===op)
             //start n2
             opReached = true;
             
             else {
                 n2 = n2 + item;
                }
            }        
    });
        console.log("n1 is ", n1);
        console.log("nu2 is ", n2);
        console.log("op is ", op);
       // console.log(opReached, "opReached is ");
        // if(opReached === true){
        //     if(containsOp(n1)=== true){
        //         console.log("n1 before", n1);
        //         n1 = doParse(n1);
        //         console.log("n1 after parse", n1);
        //     }
        if(opReached === true){   
             let ans = calculate(n1, n2, op);
             return ans;  
        }      else return parseFloat(n1);
}
    function calculate(num1, num2, op){
        console.log("num1 is ", num1);
        console.log("num2 is ", num2);
        console.log("op is ", op);
        let answer = "";
        switch(op){
            case '+':
                answer = parseFloat(num1) + parseFloat(num2);
              //  console.log('answer is', answer.toFixed(4));
                break;
            case '-':
                answer = parseFloat(num1) - parseFloat(num2);
                //console.log(answer, " in a minus")
                break;
            case '/':
                answer = parseFloat(num1) / parseFloat(num2);

                break;
            case '*':
                answer = parseFloat(num1) * parseFloat(num2);
                break;
        }   
     return answer;   
    }   

    function doScr(arr){
            let myIndex = arr.lastIndexOf(op);
            let list = arr.slice(myIndex + 1, arr.length);
            console.log(list);
            console.log(list.join(''));
            return list.join('');
    }

    function reset(){
                    result = "";
                    entries = [];
                    op = "";
    }
            
          
!
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.
Loading ..................

Console