Click here to Skip to main content
15,887,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I programmed a calculator as an exercise, but I'm just nowhere near it (don't know what I did wrong). Can anyone help me?


What I have tried:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Montserrat', sans-serif;
            margin: 0;
        }
        table {
            width: 100%;
            height: 70vh;
            color: white;
            background-color: rgb(25, 25, 25);
        }

        td {
            width: 25%;
            text-align: center;
            background-color: rgb(20, 20, 20);
            font-size: 24px;
        }

        td:hover {
            background-color: rgb(30, 30, 30);
            cursor: pointer;
        }

        #resultArea {
            height: 30vh;
            background-color: rgb(40, 40, 40);
            color: white;
            font-size: 64px;
            display: flex;
            justify-content: flex-end;
            align-items: flex-end;
            padding: 24px;
            box-sizing: border-box;
        }

        #result {
            background-color: rgb(67, 55, 236);
        }

        #result:hover {
            background-color: rgb(110, 101, 235);
        }

        .highlight {
            background-color: rgb(25, 25, 25);
        }
    </style>
    <script>

        function appendOperation(operation) {
            document.getElementById("resultArea").innerHTML =+ operation;
        }
    </script>
</head>

<body>
    <div id="resultArea">
        1234
    </div>


    <table>
        <tr>
            <td onclick="apperOperation(7)">7</td>
            <td>8</td>  
            <td>9</td>
            <td class="highlight">/</td>
        </tr>


        <tr>
            <td>4</td>
            <td>5</td>
            <td>6</td>
            <td class="highlight">x</td>
        </tr>


        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td class="highlight">+</td>
        </tr>


        <tr>
            <td>0</td>
            <td>,</td>
            <td id="result">=</td>
            <td class="highlight">-</td>
        </tr>

    </table>
</body>

</html>
Posted
Updated 8-Jan-22 6:59am
Comments
0x01AA 8-Jan-22 11:37am    
Have a look here and get some ideas: HTML | Calculator - GeeksforGeeks[^]
Btw. you tagged your question as java but it is more html and javascript ;)

1 solution

That is a lot of dead code (and a whole range of blackness). You took some care of how it looks, but you completely neglected functionality.
There is only a single onclick() and it refers to a non-existing function.

Suggestion: develop incrementally
step-by-step
start with a small piece of code (max 20 lines), focus on functionality
while (not done) {
     test and make sure its functionality is perfect (but incomplete)
     add some code and functionality
}
now do the same for layout and style: add some code
while (not done) {
     test and make sure what you added does what you intended
     add some more code and style 
} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900