You made it! By now you should have a really firm grasp on the fundamentals of JavaScript. Of course there's plenty more to learn, but you should be able to create quite a bit at this point. Our final project is going to try to combine everything you've learned so far.. you're going to be making an on-screen calculator using JavaScript, HTML and CSS.
As usual with these things there are elements of this project that are not going to be trivially easy for you, but if you've been following the course so far you definitely have everything you need to finish it. We're going to walk you through the various steps you can take, but again, how you actually implement them is up to you!
- Your calculator is going to contain functions for all of the basic math operators you typically find on simple calculators, so start by creating functions for the following items and testing them in your browser's console.
- add
- subtract
- multiply
- divide
- Create a new function
operate
that takes an operator and 2 numbers and then calls one of the above functions on the numbers. - Create a basic HTML calculator with buttons for each digit, each of the above functions and an "Equals" key.
- Do not worry about wiring up the JS just yet.
- There should also be a display for the calculator, go ahead and fill it with some dummy numbers so you can get it looking right.
- Add a "clear" button.
- Create the functions that populate the display when you click the number buttons... you should be storing the 'display value' in a variable somewhere for use in the next step.
- Make the calculator work! You'll need to store the first number that is input into the calculator when a user presses an operator, and also save which operation has been chosen and then
operate()
on them when the user presses the "=" key.- You should already have the code that can populate the display, so once
operate()
has been called, update the display with the 'solution' to the operation. - This is the hardest part of the project. You need to figure out how to store all the values and call the operate function with them. Don't feel bad if it takes you a while to figure out the logic.
- You should already have the code that can populate the display, so once
- Gotchas: watch out for and fix these bugs if they show up in your code:
- Users should be able to string together several operations and get the right answer:
12 + 7 - 5 * 3
etc. - You should round answers with long decimals so that they don't overflow the screen.
- Pressing
=
before entering all of the numbers or an operator could cause problems! - Pressing "clear" should wipe out any existing data.. make sure the user is really starting fresh after pressing "clear"
- Display a snarky error message if the user tries to divide by 0... don't let it crash your calculator!
- Users should be able to string together several operations and get the right answer:
- EXTRA CREDIT: Users can get floating point numbers if they do the math required to get one, but they can't type them in yet. Add a
.
button and let users input decimals! Make sure you don't let them type more than one though:12.3.56.5
. It is hard to do math on these numbers. (disable the decimal button if there's already one in the display) - EXTRA CREDIT: Make it look nice! This can be a good portfolio project... but not if it's UGLY. At least make the operations a different color from the keypad buttons.
- EXTRA CREDIT: Add a "backspace" button, so the user can undo if they click the wrong number.
- EXTRA CREDIT: Add keyboard support!
Submit a solution with a pull request to this file on the Javascript Curriculum github repository. See the section on Contributing for how.
- Add your solution below this line!
- Trevor's Solution - View in Browser
- Ochuko's Solution - View in Browser
- Tobenski's Solution - View in Browser
- Chris Wegscheid's Solution - View in Browser
- KoutselakisMano's Solution - View in Browser
- Hummeldon's Solution - View in Browser
- Teng Lin's Solution - View in browser
- Jacobo Martinez's Solution - View in Browser
- NatSydenham's Solution - View in Browser
- Smetanca52's Solution - View in browser
- Mohamed Elattar's Solution - View in browser
- CraigBlair's Solution - View in browser
- prw001's Solution
- Simon Tharby's solution - View in browser
- Wahaj Javed's Solution - View in Browser -Ajani Stewart's Solution - View in Browser
- Chris MacSwan's Solution - View in Browser
- ebosi's solution - View in Browser
- Max Garber's Solution - View in Browser
- Inuhoo's Solution - View in Browser
- Chad Kreutzer's Solution - View in Browser
- Tim Dowd's Solution - View in Browser
- Kevin Mulhern's Solution - View in Browser
- ruanha's solution - View in Browser
- AlienSjit's solution - View in Browser
- scarey18's solution - View in Browser
- jstnlester's Solution - View in Browser
- SarfrazAnjum's solution - View in Browser
- Ngo Van Huong's Solution - View in Browser
- Mark Bungeroth's Solution - View in Browser
- autumnchris's Solution - View in Browser
- Douglasbsx Solution - View in Browser
- Ed Magalhaes' solution - View in browser
- Tarah's Solution - View in Browser
- Alekseev Pavel's Solution - View in browser
- Adrien Pardo's Solution - View in browser
- Andrew's Solution - View in Browser
- leosoaivan's solution - View in Browser
- Johan Morin's solution - View in Browser
- mandimh's solution - View in Browser
- Alexander John's solution - View in Browser
- Austin Fisher's solutoin - View in Browser
- Jonakirke94's solution - View in Browser
- Jon Yoo's solution - View in Browser
- Javier Machin's solution - View in Browser
- Ryafl's solution - View in Browser
- Calvin's solution - View in Browser
- Nate Dimock's solution - View in Browser
- Jonathan Yiv's solution - View in Browser (not Mobile)
- Jmooree30's solution - View in Browser
- Mtizim's solution - View in Browser
- JocelynOcelyn's solution - View in Browser
- OthmanAmoudi's solution - View in Browser
- 105ron's solution - View in browser
- codyloyd's solution - View in browser
- dejanmijatovic's solution
- PongtheGreat's solution - View in browser
- Alexander Luna's solution - View in Browser
- Dareon4's solution - View in Browser
- Niko Caron's solution - View in Browser
- g0-0py's solution - View in Browser
- Jakub Cisowski's solution - View in Browser
- Eduardo Massarani's solution - View in Browser
- Naveen M V's solution - View in Browser
- AttilaTheHen's solution - View in Browser
- Laura Ilona's solution - View in Browser
- SamJamCul's solution - View in Browser
- simplybretterson's solution - View in Browser
- vartanbeno's solution - View in browser
- Zach Coursey's solution - View in browser
- HSaad's solution - View in browser
- Josiah's solution - View in browser
- Bridget Nyirongo's solution - View in browser
- CurmudJim's solution - View in Browser
- Joe Weston's solution - View in Browser
- Technicolor1's solution - View in Browser
- Encolpius's solution - View in Browser
- Lucas Manzano's solution - View in Browser
- 3lux's solution - View in Browser
- Punnadittr's solution - View in Browser
- Fabio's solution - View in Browser
- Mojotron's solution - View in Browser
- Benny's Solution - View in Browser
- Jarred Herrington's Solution - View in Browswer
- Danie Ortea's Solution - View in Browser
- Blake Bryant's solution - View in Browser
- Josh Hansen's solution - View in Browser
- AREEBAISHTIAQ's solution
- Dima Konoval's Solution - View in Browser
- Anjali Deshwani's Solution - View in Browser
- James Thomson's solution - View in Browser
- Caner Sezgin's solution - View in Browser
- Albert Garde's solution - View in Browser
- Mike Smith's solution - View in Browser
- Ben Smyth's Solution - View in Browser
- Katineto's Solution - View in Browser
- 00Saad's Solution - View in Browser
- Akash's Solution - View in Browser
- Adhithyan's Solution - View in Browser
- John Piatras' Solution - View in Browser
- Kristap's Solution - View in Browser
- Tyler's Solution - View in Browser
- spankie's Solution - View in Browser
- Rudi Boshoff's Solution - View in Browser
- thesofakillers' Solution - View in Browser
- Amy Smith's Solution - View in Browser
- Brendaneus' Solution - View in Browser
- MrObele' Solution - View in Browser
- Vyhn' Solution - View in Browser
- dmarkiewicz's Solution
- Tommy's Solution - View in Browser
- codyMalcolm's Solution - View in Browser
- Benidzu's Solution - View in Browser
- Emil Dimitrov's Solution - View in Browser
- bchalman's Solution - View in Browser
- AncherHisogi's Solution - View in Browser
- Roshan's Solution - View in Browser
- Mark Bailey's Solution - View in Browser
- Fan's Solution - View in Browser