Wednesday, September 16, 2020

Calculator Web App

 Index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header id="header">
        <h1>Welcome to MyCalci</h1>
    </header>
    <div id="section">
        <input type="text" id="screen" readonly>
        <table>
            <tr>
                <td><button>(</button></td>
                <td><button>)</button></td>
                <td><button>C</button></td>
                <td><button>%</button></td>
            </tr>
            <tr>
                <td><button>7</button></td>
                <td><button>8</button></td>
                <td><button>9</button></td>
                <td><button>X</button></td>
            </tr>
            <tr>
                <td><button>4</button></td>
                <td><button>5</button></td>
                <td><button>6</button></td>
                <td><button>+</button></td>
            </tr>
            <tr>
                <td><button>1</button></td>
                <td><button>2</button></td>
                <td><button>3</button></td>
                <td><button>-</button></td>
            </tr>
            <tr>
                <td><button>0</button></td>
                <td><button>.</button></td>
                <td><button>\</button></td>
                <td><button>=</button></td>
            </tr>
        </table>
    </div>
    <script src="index.js"></script>
</body>
</html>







style.css:

#header{
    displayblock;
    width50%;
    marginauto;
    colorpurple;
    text-aligncenter;
    border2px solid yellow;
    background-colorgold;
    border-radius10px;
    border2px solid black;
}

#section{
    text-aligncenter;
    margin-top30px;
    backgroundlinear-gradient(to bottom,purple,pink);
    widthfit-content;
    margin30px auto;
    padding30px;
    border-radius20px;

}


input{
    font-size30px;
    border2px solid red;
    backgroundlinear-gradient(to right,#000,#434343);
    border-radius10px;
    text-aligncenter;
    box-shadow2px red;
    colorwhite;
    outlinenone;
}

input:focus{
    width220px;
    background-coloryellowgreen;
}


table{
    margin20px auto;

}

button{
    font-size24px;
    backgroundlinear-gradient(#000,#434343);
    colorwhite;
    height52px;
    width52px;
    border2px solid gold;
    border-radius20px;
    outlinenone;
}

button:hover{
    backgroundgray;
}



Index.js:

let screen = document.getElementById('screen');

let buttons = document.querySelectorAll('button');

let screenValue = '';
let stackValue = '';

for(item of buttons){
    item.addEventListener('click',(e)=>{
        let buttonText = e.target.innerText;
        console.log(buttonText);

        if(buttonText == 'X'){
            buttonText = '*';
            screenValue = buttonText;
            stackValue += buttonText;
            screen.value +=screenValue;
        }
        else if(buttonText == '%'){
            buttonText = '/';
            screenValue = buttonText;
            stackValue += buttonText;
            screen.value += screenValue;
            
        }
        else if(buttonText == 'C'){
            screenValue = "";
            stackValue = "";
            screen.value = screenValue;
        }
        else if(buttonText == '='){
            screen.value = eval(stackValue);
        }
        else{
            screenValue = buttonText;
            stackValue += buttonText;
            screen.value += screenValue;
        }
        
    })
}







 

No comments:

Post a Comment