Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. // /**** sample *****/
  2.  
  3. function addition(first = 0, second = 0) {
  4. var a = parseFloat(first);
  5. var b = parseFloat(second);
  6. var total = 0;
  7. total = a + b;
  8. return total;
  9. }
  10.  
  11. function subtraction(first = 0, second = 0) {
  12. var a = parseFloat(first);
  13. var b = parseFloat(second);
  14. var sub = a - b;
  15. return sub;
  16. }
  17.  
  18. function multiplication(first = 0, second = 0) {
  19. var a = parseFloat(first);
  20. var b = parseFloat(second);
  21. var product = a * b;
  22.  
  23. return product;
  24. }
  25.  
  26. function division(first = 0, second = 0) {
  27. var a = parseFloat(first);
  28. var b = parseFloat(second);
  29. var divi = a / b;
  30. return divi;
  31. }
  32.  
  33. function get_exploded(result,operator){
  34. var holder = 0;
  35. if(result.length <= 1 ){
  36. holder = 0;
  37. }
  38. else if(result.length == 2){
  39. r1 = ((result[0]) ? result[0] : 0);
  40. if(isNaN(r1)){
  41. finder = /(\*|\+|\*|\/|\-)/g;
  42. finder_result = result[0].match(finder);
  43. if(finder_result.length > 0){
  44. exploded_new = result[0].split(""+finder_result+"");
  45. r1_new = ((exploded_new[0]) ? exploded_new[0] : 0);
  46. r2_new = ((exploded_new[1]) ? exploded_new[1] : 0);
  47. r1 = process_operator(finder_result, r1_new, r2_new);
  48. }
  49. }
  50. holder = process_operator(operator, r1, 0);
  51. }
  52. else{
  53. r1 = ((result[0]) ? result[0] : 0);
  54. r2 = ((result[1]) ? result[1] : 0);
  55. holder = process_operator(operator, r1, r2);
  56. }
  57. return holder;
  58. }
  59.  
  60. function process_operator(operator, r1, r2){
  61. var holder = 0;
  62. if(operator == '+'){
  63. holder = addition(r1, r2);
  64. }
  65. else if(operator == '-'){
  66. holder = subtraction(r1, r2);
  67. }
  68. else if(operator == '*'){
  69. if(r1 == 0 && r2 == 0){
  70. holder = 0;
  71. }
  72. else if(r1 > 0 && r2 == 0){
  73. holder = r1;
  74. }
  75. else if(r1 > 0 && r2 > 0){
  76. holder = multiplication(r1, r2);
  77. }
  78. else{
  79. holder = 0;
  80. }
  81. }
  82. else if(operator == '/'){
  83. if(r1 == 0 && r2 == 0){
  84. holder = 0;
  85. }
  86. else if(r1 > 0 && r2 == 0){
  87. holder = r1;
  88. }
  89. else if(r1 > 0 && r2 > 0){
  90. holder = division(r1, r2);
  91. }
  92. else{
  93. holder = 0;
  94. }
  95. }
  96. return holder;
  97. }
  98.  
  99. function get_input(e){
  100. var input = $(this).val();
  101. if(e.which == 107){
  102. result = input.split("+");
  103. return $(this).val(get_exploded(result,'+')+'+');
  104. }
  105. else if(e.which == 106){
  106. result = input.split("*");
  107. return $(this).val(get_exploded(result,'*')+'*');
  108. }
  109. else if(e.which == 109){
  110. result = input.split("-");
  111. return $(this).val(get_exploded(result,'-')+'-');
  112. }
  113. else if(e.which == 111){
  114. result = input.split("/");
  115. return $(this).val(get_exploded(result,'/')+'/');
  116. }
  117. else if(e.which == 13){
  118. if(isNaN(input)){
  119. result[0] = input;
  120. result[1] = 0;
  121. check_operator = /(\*|\+|\*|\/|\-)/g;
  122. operator_result = result[0].match(check_operator);
  123. return $(this).val(get_exploded(result,operator_result));
  124. }
  125. else{
  126. return $(this).val(input);
  127. }
  128. }
  129. }
  130.  
  131. $('input[type=text], input[type=number]').keyup(get_input);
  132.  
  133. // /**** end sample***/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement