Jump to content

How to make a calculator


Pradeep6868

1 post in this topic

Recommended Posts

Hi earthling,

In this series, we are going to be making a simple calculator with basic HTML, CSS and JavaScript. Our calculator will only able to perform basic math operations: addition, subtraction, multiplication and division. To better understand this tutorial you would need to have a little knowledge of HTML and CSS.

If you don’t already know them, no need to worry. I simplified this tutorial as best as I could, so you would survive :)

So what exactly do we need to build this?

As you might have already guessed, we would need to create “buttons” for inputting values and a screen for displaying these values.

Well, basically, that’s it!

But in fancier terms, these are the components we need:

· A display area for displaying operators, operands and solutions.

· Buttons for inputting values to the display screen

Visually, a calculator is a table enclosed in a container. And as you should already know tables are made of rows and columns with cells to contain table data.

1*wiLL9NX7GoGzXxSQr6nOGQ.png

Now you get the concepts, let’s get started!

This is the basic format for HTML documents.

<html>

 <head></head>

 <body>

    <! -- Content -- >

 </body>

</html>

If you don’t already understand how to deploy HTML scripts you should take a look at this short tutorial.

The visible part of a webpage goes into the <body> </body> tag.

Before I go any further, it’s essential you understand certain HTML terminologies (fancy words) such as tags, attributes and elements.

Tags: Tags are basic labels that define and separate parts of your markup into elements. They are comprised of a keyword surrounded by angle brackets <>. Content goes between two tags and the closing one is prefixed with a slash (Note: there are some self-closing HTML tags, like image tags).

<!-- This is an opening and closing paragraph tag -->

<p></p>

Attributes: A property of an HTML element used to provide additional instructions to a given HTML tag. The attribute is specified in the opening HTML tag.

<!-- Here, the class is the attribute -->

<p class=""></p>

Elements: An HTML element usually consists of a opening tag and end tag, with the content inserted in between.

<!-- The element spans from the opening tag to the closing tag -->

<p class="paragraph">This is a paragraph.</p>

Okay! Enough with the basics. Let’s dance.

The first thing that goes into our HTML body is the form element <form></form>. After it has been created, an attribute titled “name”, with the value, calculator, should then be added to the opening form tag.

<html>

 <head></head>

 <body>

    <form name=”calculator”>

    </form>

 </body>

</html>

 

The form element is to serve as a wrapper (container) for the table which will contain the main calculator components.

Okay, what’s next?

Next, we need to create the table.

So what’s a table? No, not the furniture!

Excuse my poor attempt at making a joke.

HTML tables allow web designers to arrange data like text, images, links, other tables, etc. into rows and columns of cells. They are created using the <table> tag in which the <tr> tag is used to create table rows and <td> is used to create data cells.

Row is the horizontal and column is the vertical one, by the way.

Enough talk, let’s code now.

Inside the opening and closing <form> tag, we need to create a table element.

<html>

 <head></head>

 <body>

    <form name=”calculator”>

       <table>

       </table>

    </form>

 </body>

</html>

That was a breeze, right? Great, but it sort of gets complicated now, so you might want to turn off your twitter notification and set your eyes on the screen!

F - O - C - U - S

Inside the <table> tag, we need to define the table rows <tr> which is basically going to be the horizontal section of the calculator and the table data <td> (table column cell) which is to contain the calculator display and buttons.
1*QOjYBDqPjdUrd7zxd7i4hg.jpeg

The first horizontal section of our calculator is to contain the display screen.

The second horizontal section is to contain the first set of buttons. The first one is 1, the second one is 2, the third is 3, and the fourth is the + plus sign.

The third horizontal section also contains buttons! The first one is 4, the second one is 5, the third is 6, and the fourth is the - minus sign.

Other horizontal sections? Well, they contain… more buttons!

Let’s code:

<html>

 <head></head>

 <body>

    <form name=”calculator”>

       <table>

         

          <tr>

             <td>

                <input type="text" name="display" id="display" disabled>

             </td>

          </tr>

       </table>

    </form>

 </body>

</html>

That’s for the first horizontal section, which contains the display area of the calculator.

Hey! No need to scroll up. You are right, I haven’t talked about the inputelement and every other thing in between.

So what is an input element?

An input element is a form element — the most important one at that. The input element can be displayed in several ways, depending on the typeattribute. If you are following closely, I bet you are wondering why our type attribute type = "" is set to text and not number.

Here’s your answer, a calculator not only displays numbers but it also displays operators. So the type attribute has to be set to text to accommodate both numbers and symbols (operators).

For this tutorial, I would not talk about what the id and class attribute actually are and how they used.

Finally, for the first horizontal section, there is the disabled attribute. The disabled attribute is a boolean attribute. When present, it specifies that the <input> element should be disabled. A disabled input element is unusable and un-clickable. The point of making our input element disabled, is to make the calculator button the only way users can send texts to the display. We don’t want users typing random alphabets in the display area now, do we?

Now, the second horizontal section.

Here, there are four table cell columns! So we have to use the table data <td>tag four times to define them.

<html>

 <head></head>

 <body>

    <form name=”calculator”>

       <table>

         

          <tr>

            <td>

               <input type="text" name="display" id="display" disabled>

            </td>

         </tr>

         <tr>

            <td><input type=”button” name=”one” value=”1" onclick=”calculator.display.value += ‘1’”></td>

            <td><input type=”button” name=”two” value=”2" onclick=”calculator.display.value += ‘2’”></td>

            <td><input type=”button” name=”three” value=”3" onclick=”calculator.display.value += ‘3’”></td>

            <td><input type=”button” class=”operator” name=”plus” value=”+” onclick=”calculator.display.value += ‘+’”></td>

         </tr>

      </table>

    </form>

 </body>

</html>

The first <td> tag contains the number 1, the second one is for the number 2 and so on. To make the input element a button, the type attribute should be set to button.

Whatever goes into the value attribute value = "" is what would be display on the button.

Now the onclick attribute!

If you have come this far, you should grab a bottle of beer. You deserve it.

The onclick attribute determines what is run when a click occurs. It causes a block of JavaScript code to run.

For this tutorial, I would not go deep into the use of the onclick attribute.

Essentially, the code contained in the onclick attribute is simply telling the web browser to display whatever value the button holds when it is clicked.

The actual value of the input is only modified with the += (append to). If the current calculator input is 5 + and 10 is clicked, 10 will be appended: it then becomes 5 + 10, which is 15. Without the “append to” symbol, the number 10 will override 5. And only 10 will be displayed on the calculator screen.

The content for the second horizontal section should be repeated for the third and the fourth row. All that would be changed is the input value.

But things are going to be a little different for the fifth row.

Why?

The equals to function!

The onclick attribute onclick = "" of the equals to function would contain something a little different from the rest.

<td>

<input type=”button” name=”doit” value=”=”            onclick=”calculator.display.value = eval(calculator.display.value)”></td>

What eval(calculator.display.value) does is to interpret the value of the input as Javascript. So when there is a 4 + 6 in the input, it’ll be evaluated as Javascript and return 10.

Yippee!

That wasn’t so hard, now was it?

When all the individual components are pieced together, we have a fully functional (not so attractive) calculator.

Link to comment
https://iosgods.com/topic/69140-how-to-make-a-calculator/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Our picks

    • Idle Mine Dig: Drill & Collect v1.19.03 +7 [Currency Hack]
      Modded/Hacked App: Idle Mine Dig: Drill & Collect By APPS TEKNOLOJI ANONIM SIRKETI
      Bundle ID: tr.com.apps.drill.and.collect
      iTunes Store Link: https://apps.apple.com/us/app/idle-mine-dig-drill-collect/id1642304873?uo=4

       

      📌 Mod Requirements

      - Jailbroken iPhone or iPad.
      - iGameGod / Filza / iMazing.
      - Cydia Substrate, ElleKit, Substitute or libhooker depending on your jailbreak (from Sileo, Cydia or Zebra).

       

      🤩 Hack Features

      - No Ads
      - VIP Active
      - Freeze Currency
      - Freeze Pills
      - Freeze Tickets
      - Freeze Tokens
      - All Costumes Unlocked

       

      ⬇️ iOS Hack Download Link


      • 0 replies
    • Idle Mine Dig: Drill & Collect v1.19.03 +7 Jailed Cheats [Currency Hack]
      Modded/Hacked App: Idle Mine Dig: Drill & Collect By APPS TEKNOLOJI ANONIM SIRKETI
      Bundle ID: tr.com.apps.drill.and.collect
      iTunes Store Link: https://apps.apple.com/us/app/idle-mine-dig-drill-collect/id1642304873?uo=4

       

      📌 Mod Requirements

      - Non-Jailbroken/Jailed or Jailbroken iPhone or iPad.
      - Sideloadly or alternatives.
      - Computer running Windows/macOS/Linux with iTunes installed.

       

      🤩 Hack Features

      - No Ads
      - VIP Active
      - Freeze Currency
      - Freeze Pills
      - Freeze Tickets
      - Freeze Tokens
      - All Costumes Unlocked

       

      ⬇️ iOS Hack Download IPA Link


      • 0 replies
    • I Am Your Beast v1.0.3 +5 Jailed Cheats [ God Mode ]
      Modded/Hacked App: I Am Your Beast By Frosty Pop Games Inc.
      Bundle ID: com.frostypop.beast
      iTunes Store Link: https://apps.apple.com/us/app/i-am-your-beast/id6473058219?uo=4

       


      🤩 Hack Features

      - God Mode
      - Unlimited Ammo
      - Dumb Enemies
      - Disable Enemy Radio
      - Free In-App Purchases
      • 0 replies
    • I Am Your Beast v1.0.3 +5 Cheats [ God Mode ]
      Modded/Hacked App: I Am Your Beast By Frosty Pop Games Inc.
      Bundle ID: com.frostypop.beast
      iTunes Store Link: https://apps.apple.com/us/app/i-am-your-beast/id6473058219?uo=4

       


      🤩 Hack Features

      - God Mode
      - Unlimited Ammo
      - Dumb Enemies
      - Disable Enemy Radio
      - Free In-App Purchases
        • Haha
      • 2 replies
    • Honkai Impact 3 (SEA) Cheats v8.1.0 +2 [ Multiply Attack & Defense ]
      Modded/Hacked App: Honkai Impact 3 By COGNOSPHERE PTE. LTD.
      Bundle ID: com.miHoYo.bh3oversea
      iTunes Store Link: https://apps.apple.com/ph/app/honkai-impact-3/id1299582178?uo=4

       

      📌 Mod Requirements

      - Jailbroken iPhone or iPad.
      - iGameGod / Filza / iMazing.
      - Cydia Substrate, ElleKit, Substitute or libhooker depending on your jailbreak (from Sileo, Cydia or Zebra).

       

      🤩 Hack Features

      - Multiply Attack
      - Multiply Defense

       

      ⬇️ iOS Hack Download Link: https://iosgods.com/topic/134285-honkai-impact-3-sea-cheats-v810-2-multiply-attack-defense/
        • Agree
        • Winner
        • Like
      • 491 replies
    • [Puzzles & Dragons JP] パズル&ドラゴンズ v22.0.1 Jailed Cheats +2
      Modded/Hacked App: パズル&ドラゴンズ By GungHo Online Entertainment, INC.
      Bundle ID: jp.gungho.pad
      iTunes Store Link: https://apps.apple.com/jp/app/%E3%83%91%E3%82%BA%E3%83%AB-%E3%83%89%E3%83%A9%E3%82%B4%E3%83%B3%E3%82%BA/id493470467?uo=4


      Hack Features:
      - God Mode
      - One Hit Kill


      Jailbreak required hack(s): https://iosgods.com/topic/133984-puzzle-dragons-japan-english-cheats-all-versions-3/


      iOS Hack Download Link: https://iosgods.com/topic/135539-puzzles-dragons-jp-%E3%83%91%E3%82%BA%E3%83%AB%EF%BC%86%E3%83%89%E3%83%A9%E3%82%B4%E3%83%B3%E3%82%BA-v1852-jailed-cheats-2/
        • Like
      • 383 replies
    • Honkai Impact 3rd Cheats v8.1.0 +2 [ Multiply Attack & Defense ]
      Modded/Hacked App: Honkai Impact 3rd By COGNOSPHERE PTE. LTD.
      Bundle ID: com.miHoYo.bh3global
      iTunes Store Link: https://apps.apple.com/us/app/honkai-impact-3rd/id1336342304?uo=4

       

      📌 Mod Requirements

      - Jailbroken iPhone or iPad.
      - iGameGod / Filza / iMazing.
      - Cydia Substrate, ElleKit, Substitute or libhooker depending on your jailbreak (from Sileo, Cydia or Zebra).

       

      🤩 Hack Features

      - Multiply Attack
      - Multiply Defense

       

      ⬇️ iOS Hack Download Link: https://iosgods.com/topic/134276-honkai-impact-3rd-cheats-v810-2-multiply-attack-defense/
        • Agree
        • Winner
        • Like
      • 620 replies
    • Cat Snack Bar Cheats v1.0.164 +1
      Modded/Hacked App: Cat Snack Bar By treeplla Inc.
      Bundle ID: com.tree.idle.catsnackbar
      iTunes Store Link: https://apps.apple.com/us/app/cat-snack-bar/id6443895159?uo=4


      Hack Features:
      - Freeze Currencies


      Non-Jailbroken & No Jailbreak required hack(s): https://iosgods.com/topic/170232-cat-snack-bar-v1036-jailed-cheats-1/


      iOS Hack Download Link: https://iosgods.com/topic/170233-cat-snack-bar-cheats-v1036-1/
        • Informative
      • 72 replies
    • My Cafe — Restaurant game Cheats v2025020.4.681 +3
      Modded/Hacked App: My Cafe — Restaurant game by Melsoft
      Bundle ID: com.Melesta.MyCafe
      iTunes Store Link: https://apps.apple.com/us/app/my-cafe-restaurant-game/id1068204657?uo=4&at=1010lce4


      Mod Requirements:
      - Jailbroken iPhone/iPad/iPod Touch.
      - iFile / Filza / iFunBox / iTools or any other file managers for iOS.
      - Cydia Substrate or Substitute.
      - PreferenceLoader (from Cydia or Sileo).


      Hack Features:
      - Unlimited Gems
      - VIP Level 7 (probably visual)


      Notes:
      - To get unlimited gems, just get some gems from the customers.
      - If you purchase using gems from certain places like the VIP Store, it will cause you to restart the game
      - If you have "a lot" gems, you can turn off cheats to see if it works without the hax.
      - "a lot" means a lot of gems 

      Non-Jailbroken & No Jailbreak required hack(s): https://iosgods.com/forum/79-no-jailbreak-section/
      Modded Android APK(s): https://iosgods.com/forum/68-android-section/
      For more fun, check out the Club(s): https://iosgods.com/clubs/


      iOS Hack Download Link:

      Hidden Content
      Download Hack







      Installation Instructions:
      STEP 1: Download the .deb Cydia hack file from the link above.
      STEP 2: Copy the file over to your iDevice using any of the file managers mentioned above or skip this step if you're downloading from your iDevice.
      STEP 3: Using iFile or Filza, browse to where you saved the downloaded .deb file and tap on it.
      STEP 4: Once you tap on the file, you will then need to press on 'Installer' or 'Install' from the options on your screen.
      STEP 5: Let iFile / Filza finish the cheat installation. Make sure it successfully installs, otherwise see the note below.
      STEP 6: Now open your iDevice settings and scroll down until you see the settings for this cheat and tap on it. If the hack is a Mod Menu, the cheat features can be toggled in-game.
      STEP 7: Turn on the features you want and play the game. You may need to follow further instructions inside the hack's popup in-game.

       

      NOTE: If you have any questions or problems, read our Troubleshooting topic & Frequently Asked Questions topic. If you still haven't found a solution, post your issue down below and we'll do our best to help! If the hack does work for you, post your feedback below and help out other fellow members that are encountering issues.


      Credits:
      - @Zahir


      Cheat Video/Screenshots:
        • Informative
        • Agree
        • Haha
        • Winner
        • Like
      • 639 replies
    • Monster Legends: Collect all Cheats v17.8 +8
      Modded/Hacked App: Monster Legends: Merge RPG By Socialpoint
      Bundle ID: es.socialpoint.MonsterCity
      iTunes Store Link: https://apps.apple.com/us/app/monster-legends-merge-rpg/id653508448?uo=4

       

      📌 Mod Requirements

      - Jailbroken iPhone or iPad.
      - iGameGod / Filza / iMazing.
      - Cydia Substrate, ElleKit, Substitute or libhooker depending on your jailbreak (from Cydia, Sileo or Zebra).

       

      🤩 Hack Features

      - 1 Hit Kill
      - Skip Enemy Turn
      - Multiply Attack
      - Multiply Defense
      - Insane Score (Always 3 Stars)
      - No Skill Cost
      - Auto Win
      - Auto Play Battle Enabled for All Maps


      🍏 For Non-Jailbroken & No Jailbreak required hacks: https://iosgods.com/topic/140543-monster-legends-collect-all-v1778-5-cheats-for-jailed-idevices/

       

      ⬇️ iOS Hack Download Link: https://iosgods.com/topic/176914-monster-legends-collect-all-cheats-v1779-8/
        • Agree
        • Haha
        • Winner
        • Like
      • 311 replies
    • Rent Please! Landlord Sim Cheats v1.5.4 +2
      Modded/Hacked App: Rent Please! Landlord Sim By Shimmer Games Co., Ltd.
      Bundle ID: com.shimmergames.tenants.gb.en
      iTunes Store Link: https://apps.apple.com/us/app/rent-please-landlord-sim/id1645842987?uo=4


      Hack Features:
      - Infinite Currencies
      - No Ads


      Non-Jailbroken & No Jailbreak required hack(s): https://iosgods.com/topic/168311-rent-please-landlord-sim-v111-jailed-cheats-2/


      iOS Hack Download Link: https://iosgods.com/topic/168312-rent-please-landlord-sim-cheats-v111-2/
        • Informative
        • Haha
        • Like
      • 134 replies
    • Angry Birds 2 Cheats v3.27.2 +1 [ Infinite Currencies ]
      Modded/Hacked App: Angry Birds 2 By Rovio Entertainment Oyj
      Bundle ID: com.rovio.baba
      iTunes Store Link: https://apps.apple.com/us/app/angry-birds-2/id880047117?uo=4


      Hack Features:
      - Infinite Currencies ( Spend some/ Get some )


      Non-Jailbroken & No Jailbreak required hack(s):  https://iosgods.com/topic/70081-angry-birds-2-v2600-jailed-cheats-2/


      Hack Download Link: https://iosgods.com/topic/72039-angry-birds-2-cheats-v2600-1-infinite-currencies/
        • Informative
        • Haha
        • Thanks
        • Winner
        • Like
      • 1,935 replies
×
  • Create New...

Important Information

We would like to place cookies on your device to help make this website better. The website cannot give you the best user experience without cookies. You can accept or decline our cookies. You may also adjust your cookie settings. Privacy Policy - Guidelines