Code Monkey home page Code Monkey logo

react-state-calculator's Introduction

React State Simple Calculator

In this exercise you build a simplified calculator application using React.

Learning Objectives

  • Build a component that uses a useState hook to keep component state.
  • Build a component with an onClick attribute that has a value of the function to invoke when the event is triggered
  • Model the data a React application needs and create state hooks accordingly.

Set up

  • Fork and clone this repository
  • npm ci to install dependencies
  • npm run dev to start the React development server

Requirements

You are required to implement a simplified calculator app. Image

  • When the user presses a numbered button on either of the panels, the display above should be updated to show that number.
  • When user presses the clear button, the display should be set back to 0
  • When the user presses one of the operation buttons (+, -,*,÷) the operation display should be updated to show that operation.
  • When the user presses the equals button the answer display should be updated to show the result of the calculation.

Tips

  • Start by modelling the state of the application
  • What state will you need to keep track of?
  • What user interactions will modify that state and how?
  • Start with one or two functional buttons and only consider the add operation. When you have that working, make additional buttons functional.

Extension 1

  • Update your solution so the number panels act like a real calculator when inputting numbers
    • Numbers should appended, so pressing 1 then 2 should result in 12 being displayed
    • If the user presses a number while '0' is displayed, the number should replace '0' on the display. Subsequent numbers, including 0, are appended on to the display as usual.
    • If the user presses '0' while '0' is displayed, the display should remain at '0'. It should not be possible to define a number with leading zeros.

Extension 2

  • Add a "store" button underneath the answer.
  • When this button is clicked, the current answer should be stored in a state variable.
  • Add a "recall" button to each of the number displays. When this button is clicked, the currently stored value should be shown on the corresponding number panel.

Extension 3

  • Add a "." button to the numbers panel of each section
  • When the user presses this button, a decimal separator should be added to the number
  • Only a single separator can be added to each number
  • If no digit appears after the separator, the calculator should consider the number as having no separator (i.e. "1." should be treated as "1").
  • Unlike whole numbers, leading zeros should be allowed after the separator. For example, "00001" is not valid, but "0.00001" is and "1.00001" is.

react-state-calculator's People

Contributors

julesnuggy avatar mikemherron avatar chris2733 avatar guro18 avatar dearshrewdwit avatar logi13 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.