Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. $(document).ready(function () {
  2. var result = 0;
  3. var prevEntry = 0;
  4. var operation = null;
  5. var currentEntry = '0';
  6. updateScreen(result);
  7.  
  8. $(".button").on("click", function () {
  9. var currentvalue = $(this).html()
  10. console.log(currentvalue);
  11. if (isNumber(currentvalue)) {
  12. if (currentEntry === '0') currentEntry = currentvalue;
  13. else currentEntry = currentEntry + currentvalue;
  14. result = currentEntry;
  15. updateScreen(result);
  16. }
  17. else if (isOperator(currentvalue)) {
  18. prevEntry = parseFloat(currentEntry);
  19. operation = currentvalue;
  20. console.log(operation);
  21. currentEntry = '';
  22. result = currentEntry;
  23. updateScreen(result);
  24.  
  25. }
  26. else if (currentvalue === '=') {
  27. currentEntry = operate(prevEntry, currentEntry, operation);
  28. operation = null;
  29. updateScreen(currentEntry);
  30. }
  31. });
  32.  
  33.  
  34.  
  35.  
  36. function isNumber(number) {
  37. return !isNaN(number)
  38. }
  39.  
  40. isOperator = function (value) {
  41. return value === '/' || value === '*' || value === '+' || value === '-';
  42. };
  43.  
  44. operate = function (a, b, operation) {
  45. a = parseFloat(a);
  46. b = parseFloat(b);
  47. console.log(a, b, operation);
  48. if (operation === '+') return a + b;
  49. if (operation === '-') return a - b;
  50. if (operation === '*') return a * b;
  51. if (operation === '/') return a / b;
  52. }
  53.  
  54.  
  55. function updateScreen(displayValue) {
  56. var displayValue = displayValue.toString();
  57. $('.screen').val(displayValue.substring(0, 10));
  58.  
  59. }
  60. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement