Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- var result = 0;
- var prevEntry = 0;
- var operation = null;
- var currentEntry = '0';
- updateScreen(result);
- $(".button").on("click", function () {
- var currentvalue = $(this).html()
- console.log(currentvalue);
- if (isNumber(currentvalue)) {
- if (currentEntry === '0') currentEntry = currentvalue;
- else currentEntry = currentEntry + currentvalue;
- result = currentEntry;
- updateScreen(result);
- }
- else if (isOperator(currentvalue)) {
- prevEntry = parseFloat(currentEntry);
- operation = currentvalue;
- console.log(operation);
- currentEntry = '';
- result = currentEntry;
- updateScreen(result);
- }
- else if (currentvalue === '=') {
- currentEntry = operate(prevEntry, currentEntry, operation);
- operation = null;
- updateScreen(currentEntry);
- }
- });
- function isNumber(number) {
- return !isNaN(number)
- }
- isOperator = function (value) {
- return value === '/' || value === '*' || value === '+' || value === '-';
- };
- operate = function (a, b, operation) {
- a = parseFloat(a);
- b = parseFloat(b);
- console.log(a, b, operation);
- if (operation === '+') return a + b;
- if (operation === '-') return a - b;
- if (operation === '*') return a * b;
- if (operation === '/') return a / b;
- }
- function updateScreen(displayValue) {
- var displayValue = displayValue.toString();
- $('.screen').val(displayValue.substring(0, 10));
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement