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.

            
              <!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Calculator</title>
  <meta name="description" content="The HTML5 Herald">
  <meta name="author" content="SitePoint">

  <link rel="stylesheet" href="styles.css">

</head>

<body>
    
       
  <div class = "container">
        <div id = "display"></div> 
        <div class = "keys">   
            <div class = "griditem number" id = "one">1</div>
            <div class = "griditem number" id = "two">2</div>
            <div class = "griditem number" id = "three">3</div>
            <div class = "griditem operator" id = "plus">+</div>
            <div class = "griditem number" id = "four">4</div>
            <div class = "griditem number" id = "five">5</div>
            <div class = "griditem number" id = "six">6</div>
            <div class = "griditem operator" id = "minus">-</div>
            <div class = "griditem number" id = "seven">7</div>
            <div class = "griditem number" id = "eight">8</div>
            <div class = "griditem number" id = "nine">9</div>
            <div class = "griditem operator" id = "multiply">*</div>
            <div class = "griditem reset" id = "back">Back</div>
            <div class = "griditem number" id = "zero">0</div>
            <div class = "griditem reset" id = "clear">CLR</div>
            <div class = "griditem operator" id = "division">/</div>
            <div class = "griditem" ></div>
            <div class = "griditem" id = "decimal" >.</div>
            <div class = "griditem" ></div>
            <div class = "griditem equals" id = "equals">=</div>
        </div>
</div>



  <script src="scripts.js"></script>
</body>
</html>
            
          
!
            
              .keys {
  display: grid;
  grid-template-columns: 25% 25% 25% 25%;
  grid-template-rows: 70px 70px 70px 70px 70px;
  padding: 10px;
}

.operator {
    background: #0686ee;
}
.number {
    background: #99d6ff;
}
#decimal {
    background: #99d6ff;  
}
.reset {
    background: #0686ee;
}
.equals {
    background: #0686ee;
}
.griditem {
    border-radius: 25px;
    padding: 5px;
    margin: 5px;
    border: 1px solid black;
    display:inline-flex;
    justify-content:center;
    align-items:center;
}


#display{
    background: #adababf8;
    width: 308px;
    height: 80px;
    border-top: 1px solid black;
    border-left: 1px solid black;
    border-right: 1px solid black;
    font-size: 40px;
    display:inline-flex;
    justify-content:flex-end;
    align-items:center;
    padding: 5px;
    overflow-x: auto;
    border-radius: 15px;
}

.container{
margin: auto;
padding-top:60px;
padding-bottom: 30px;
padding-left: 30px;
padding-right: 30px;
width: 30%;
background: black;
border-radius: 25px;
text-align: center;
}

body {
    background-color: #84f107;
}

            
          
!
            
              //variables

var display = document.getElementById('display');
display.innerHTML = display.innerHTML.substring(0,300)

var operator = ' ';
var num1 = ' ';
var num2 = ' ';
var result = ' ';
var decimalon = ' ';
var decimal = document.getElementById('decimal');


//functions
let add = (num1, num2) => Number(num1) + Number(num2);
let subtract = (num1, num2) => Number(num1) - Number(num2);
let multiply = (num1, num2) => Number(num1) * Number(num2);
let divide = (num1, num2) => Number(num1) / Number(num2);

//clears nums depending on what point of the equation the user is pressing back
let back = () => {
    if( num2 !== ' '){
    num2 = ' ';
    display.textContent = ' ';
}
else if (num1 !== ' ' && num2 == ' '){
    num1 = ' ';
    display.textContent = ' ';
} 
else{
    return;
}
};

let operate = () => {
result = ' ';
display.textContent = ' ';
switch(operator){
    case '+': result = add(num1,num2);
    break;
    case '-': result = subtract(num1,num2);
    break;
    case '*': result = multiply(num1,num2);
    break;
    case '/': result = divide(num1,num2);
}
//round decimal if over 7 digits
if(result.toString().length >7){   
    display.innerText = Number(result.toFixed(7));
}
else {
display.textContent = Number(result);
}
num2= ' ';
num1= ' ';
};

//picks up the operator and calcs if there are already 2 numbers
function operatorKeys() {
if  (num1 != ' '){
    if (num2 == ' '){
        operator = this.textContent;
        display.textContent = this.textContent;
        decimalon = 'off';
    }     
    else if (num2 !== ' '){
        operate()
        display.textContent = result;
        operator = this.textContent;
        decimalon = 'off'
        num1 = result;  
    }
}
else {
    return;
}
};
//picks up the numbers when clicked
function numberKeys(){
    if (operator === ' '){
        num1 += this.textContent;
        display.textContent = num1;
        } else if (operator !== ' '){
        num2 += this.textContent;
        display.textContent = num2;
        }
}
//flicks an on switch when . has been selected to prevent multiple decimals
function addDecimal() {
    if (decimalon == 'on'){
        return;
    } else{
        if(num2 == ' ' && operator == ' '){
            decimalon = 'on';
            num1 += '\.';
            display.textContent = num1;
            console.log("num1= ",num1);
            return;
        } else {
            decimalon = 'off';
            num2 += '\.';
            decimalon = 'on';
            display.textContent = num2;
            console.log("num2= ",num2);
            return;
        }
    }

};
 
//add event listeners to all the buttons, update the display, and update nums,

var buttons = document.getElementsByClassName('number');
for (var i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('click', numberKeys);
};
//THIS IS WHERE I AM HAVING ISSUES.
//add keyboard functionality
let keyboardPress = (event) => {
    var key_press = event.key;
    console.log(event.key);
  console.log(typeof(key_press));
   console.log(event);
 // Target each keypress and update the Display screen
 if (['0','1','2','3','4','5','6','7','8','9'].includes(key_press)){
    if (operator === ' '){
        num1 += key_press;
        display.textContent = num1;
        } else if (operator !== ' '){
        num2 += key_press;
        display.textContent = num2;
        }
    }
//I think the problem is somewhere in here
 else if (['+','/','-','*'].includes(key_press)){
    console.log(operator);
    if  (num1 != ' '){
        if (num2 == ' '){
            operator = key_press.textContent;
            display.textContent = key_press.textContent;
            decimalon = 'off';
        }     
        else if (num2 !== ' '){
            operate()
            display.textContent = result;
            operator = key_press.textContent;
            decimalon = 'off'
            num1 = result;  
        }
    }
    else {
        return;
    }
    }
else if (key_press == '=' ){
    operate();
}
else if (key_press == 'Backspace'){
    back();
}

else if (key_press == '.'){
    addDecimal();
}
};

window.addEventListener('keydown',keyboardPress);


//adds event listeners to the operater keys and updates the operator 
var keys = document.getElementsByClassName('operator');
for (var i = 0; i < keys.length; i++) {
    keys[i].addEventListener('click', operatorKeys);
};


//clear, decimal, and equals buttons
document.getElementById("clear").addEventListener("click", function(e) { 
    num1 = ' ';
    num2 = ' ';
    operator = ' ';
    decimalon = ' ';
    display.textContent = ''});
document.getElementById("equals").addEventListener("click", operate); 

document.getElementById("back").addEventListener("click", back)

document.getElementById("decimal").addEventListener("click", addDecimal)


            
          
!
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