Giter Club home page Giter Club logo

js-calculator-tombousquet's Introduction

Build a Calculator

Directions

This exercise will help you understand how to use JavaScript functions and algorithms. You will also work with the DOM and respond to user input.

Step 1: Set up your project

  • Create an index.html file
  • Create a main.css file and link it to your index.html file
  • Create a main.js file and link it to your index.html file

Step 2: Build out the HTML and CSS

  • Using the mockup provided below, create your HTML and CSS.
Mockup In Action

Step 3: Write the JavaScript

This is the main part of the project. You will have to react to click events on each calculator button and update the display. Start small by making the numbers work, then move on to the operators, decimal button, and equals button.

When a user presses the equals button, evaluate the math expression they have created and put the result in the display. Further buttons pressed add operators and numbers after that result.

You do not have to stop users from entering bad input. For instance, if a user enters 2.1.0 -+ 7, you can let them enter that and do not have to show any special output when they try to evaluate it using the equals button. It is fine if you do prevent them from entering bad input, though.

You do not have to allow for chaining long calculations (like 2 + 4 * 7 - 2), but you can.

Hint: One way of evaluating mathematical expressions is eval. Another way is by using the math.js library.

Bonus steps

You should find the above project challenging. However, if you complete it, here are more features you should attempt to add in.

  • Show an error message when invalid input is given. Using try-catch is your best bet here.
  • Disallow invalid input.
  • Allow for chaining long calculations. Order of operations must be honored.
  • Handle edge situations. For example, if a user starts by pressing the decimal button, they generally expect to start a decimal number, which needs a 0 before the decimal point. Add that 0.
  • Allow entry via the keyboard, not just clicking on buttons.
  • Add a button for calculating the modulo of two numbers.
  • Add a button for raising a number by a power.
  • Add parentheses to control the order of evaluation.

js-calculator-tombousquet's People

Contributors

amygori avatar tombousquet avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.