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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="main.css">
    <title>Basic Calculator</title>
</head>
<body>
<div class="calculator">


    <input type="text" placeholder="0" class="display_screen">
    <div class="calculator-buttons">
            <button class="calc-button clearBtn">C</button>
            <button class="calc-button  btnDivide "   onclick="divide()"value="/">&divide;</button>


        <button class="calc-button button_one num" value="1">1</button>
        <button class="calc-button button_two num" value="2">2</button>
        <button class="calc-button  button_three num" value="3">3</button>
        <button class="calc-button button_minus ops" onclick="subtract()" value="-">&minus;</button>
        <button class="calc-button button_four num" value=
        "4">4</button>
        <button class="calc-button button_five num" value="5">5</button>
        <button class="calc-button button_six num" value="6">6</button>
        <button class="calc-button button_plus ops" onclick="add()" value="+">&plus;</button>
        <button class="calc-button button_seven num" value="7">7</button>
        <button class="calc-button button_eight num" value="8">8</button>
        <button class="calc-button button_nine num" value="9">9</button>
        <button class="calc-button  ops" value=".">.</button>
        
        <button class="calc-button button_times ops" onclick="multiply()" value="*">&times;</button>
        
        <button class="calc-button zeroBtn num" value="0">0</button>
        
        <button class="calc-button equalsBtn ops">&equals;</button>








    </div>
</div>
    <script src="script.js"></script>
    
</body>
</html>
              
            
!

CSS

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

body{
    background:#f6f6f6;
    padding-bottom:30px;
    padding-top:30px;
}

.calculator{
    max-width: 400px;
    margin:0 auto;
    border:3px solid black;
    border-radius:7px;
    /* padding:1.5rem; */
    /* border-radius:1rem; */
    /* width:100%; */
}

.calculator input{
    background:none;
    border:none;
    box-shadow:none;
    border-bottom: 3px solid black;
    padding:30px;
    text-align:right;
    font-size:1.8rem;
    /* border-radius:1rem; */
    width:100%;
}

.calculator-buttons{
    display:grid;
    grid-template-columns: repeat(4,1fr);
    grid-gap:20px;
    padding:20px;
    background:pink;
    width:100%;
    cursor:pointer;
}

.calc-button{
    background:#808080;
    padding:20px;
    /* border-radius:1rem; */
    font-size:1.8rem;
    cursor:pointer;
    color:#eee;


}

.clearBtn{
    background:blue;
    grid-column: span 3;
    
}

.equalsBtn{
    background:red;
    grid-column: span 2;
}

/* .zeroBtn{
    background:red;
    grid-column:span 2;
} */
              
            
!

JS

              
                

let button_equals=document.querySelector('.equalsButton');
let clearBtn=document.querySelector('.clearBtn');
let num=document.querySelectorAll('.num');
let ops=document.querySelectorAll('.ops');
let display_screen=document.querySelector('.display_screen');

let button=document.getElementsByTagName('button');



//Covert NodeList into an array
// Array.from(num).forEach(function(el){
    
//         el.addEventListener('click',function(event){
//             display_screen.value+=event.target.value;
//         })
   
   
    
// })

display_screen=document.querySelector('.display_screen');
num.forEach(function(el){
el.addEventListener('click',function(){
display_screen.value += el.value;
})
});

clearBtn.addEventListener('click',function(){
    console.log("Clear button works");
    clearDisplay();
})

// function button_equals(){
//     console.log("equals")
// }




function divide(){
    console.log("Function divides");
}

function add(){

    // console.log("Function adds");
    let result=0;
    for(let i=0; i<num.length;i++){
        result+=parseFloat(num);
    }

    // button_equals();

    return result;
    
    
    
}

function multiply(){
    console.log("Function multiplies");
    
}

function subtract(){
    console.log("Function substracts");
    
}

function clearDisplay(){
    display_screen.value=' ';

}





ops.forEach((op) => 
    op.addEventListener('click', function(){
        console.log("Ops button is clicked");

        switch(op){
            case '+':
                add();
                break;
            case '-':
                substract();
                break;
            case '*':
                multiply();
                break;
            case '/':
                divide();
                break;  
            case '=':
                button_equals();
                break;               
        }
        
    })
);

              
            
!
999px

Console