#jquery calculator:
-
create html file and include the dependencies (jquery, bootstrap);
-
create html layout on page. that includes: script, buttons (numbers, clear, operators, equals);
-
declare variables for the numbers:
- first number,
- second number
- result
-
declare variable for the operator;
-
attach an event handler to the numbers (ie $('.number').on('click', function...)) and inside the event handler (function):
- store the number pressed as a string in the first number variable
- when a number is clicked again, concatenate with what we have stored in the first number var.
- if we have an operator stored in our operator variable, then concatenate to second number var, otherwise concatenate to first number var.
-
attach an event handler to all our operators so we can store which one was picked in our operator variable.
-
attach an event handler to the equal button which will calculate the result and print to the screen based on the operator that's stored in the operator variable.
Note: infer what button was clicked using "this" which will have a reference to the button which was clicked
var operator;
// html: <button class="operator" value="+"> + </button>
$('.operator').on('click', function () {
// this === the clicked btn
operator = $(this).data('operator');
});