Comprehensive Word Problem Calculator

A versatile word problem calculator that handles various equations and unknowns

  1. // ==UserScript==
  2. // @name Comprehensive Word Problem Calculator
  3. // @namespace http://tampermonkey.net/
  4. // @version 4.5
  5. // @description A versatile word problem calculator that handles various equations and unknowns
  6. // @author Your Name
  7. // @match https://easybridge-dashboard-web.savvaseasybridge.com/dashboard/student?ssoLogin=true
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function wordProblemCalculator() {
  16. const problem = prompt("Enter a word problem (e.g., 'What number is 100 less than 765?'). You can also type 'div' for division:").toLowerCase();
  17. let result;
  18.  
  19. // Normalize "div" to "divided by"
  20. const normalizedProblem = problem.replace(/\bdiv\b/g, 'divided by');
  21.  
  22. // Handle "What number is X less than Y"
  23. if (problem.includes("what number is") && problem.includes("less than")) {
  24. const numbers = problem.match(/\d+/g);
  25. if (numbers && numbers.length >= 2) {
  26. const lessAmount = parseFloat(numbers[0]); // The amount to subtract
  27. const total = parseFloat(numbers[1]); // The number to subtract from
  28. result = total - lessAmount; // Perform the subtraction
  29. } else {
  30. alert("Could not find enough numbers.");
  31. return;
  32. }
  33. }
  34. // Handle missing value (e.g., 10 + 30 = ?)
  35. else if (normalizedProblem.includes("=") || normalizedProblem.includes("?")) {
  36. const parts = normalizedProblem.split('=');
  37. const leftSide = parts[0].trim();
  38. const rightSide = parts[1] ? parts[1].trim() : '';
  39.  
  40. // Check for unknown value on the left side
  41. if (rightSide === '?' || rightSide === '') {
  42. const matches = leftSide.match(/(\d+)\s*([-+x*/]|divided by)\s*(\d+)/);
  43. if (matches) {
  44. const num1 = parseFloat(matches[1]);
  45. const operation = matches[2];
  46. const num2 = parseFloat(matches[3]);
  47.  
  48. switch (operation) {
  49. case "+":
  50. result = num1 + num2;
  51. break;
  52. case "-":
  53. result = num1 - num2;
  54. break;
  55. case "x":
  56. case "*":
  57. result = num1 * num2;
  58. break;
  59. case "divided by":
  60. case "/":
  61. if (num2 !== 0) {
  62. result = num1 / num2;
  63. } else {
  64. alert("Cannot divide by zero.");
  65. return;
  66. }
  67. break;
  68. default:
  69. alert("Could not understand the operation.");
  70. return;
  71. }
  72. } else {
  73. alert("Could not understand the equation.");
  74. return;
  75. }
  76. }
  77. // Check for unknown value on the right side
  78. else if (leftSide.includes("?")) {
  79. const matches = rightSide.match(/(\d+)/);
  80. if (matches) {
  81. const total = parseFloat(matches[1]);
  82.  
  83. const operation = leftSide.includes('+') ? '+'
  84. : leftSide.includes('-') ? '-'
  85. : leftSide.includes('*') ? '*'
  86. : leftSide.includes('/') ? '/'
  87. : leftSide.includes('divided by') ? 'divided by'
  88. : null;
  89.  
  90. const numMatches = leftSide.match(/(\d+)/g);
  91. if (numMatches && numMatches.length === 2) {
  92. const num1 = parseFloat(numMatches[0]);
  93. const num2 = parseFloat(numMatches[1]);
  94.  
  95. switch (operation) {
  96. case "+":
  97. result = total - num1; // Solve for "?"
  98. break;
  99. case "-":
  100. result = num1 - total; // Solve for "?"
  101. break;
  102. case "*":
  103. result = total / num1; // Solve for "?"
  104. break;
  105. case "/":
  106. case "divided by":
  107. result = num1 * total; // Solve for "?"
  108. break;
  109. default:
  110. alert("Could not understand the operation for unknown value.");
  111. return;
  112. }
  113. }
  114. } else {
  115. alert("Could not find enough numbers for the equation.");
  116. return;
  117. }
  118. }
  119. else {
  120. alert("Could not understand the equation format.");
  121. return;
  122. }
  123. }
  124. // Handle standard operations (basic arithmetic)
  125. else {
  126. const matches = normalizedProblem.match(/(\d+)\s*([-+x*/]|divided by)\s*(\d+)/);
  127. if (matches) {
  128. const num1 = parseFloat(matches[1]);
  129. const operation = matches[2];
  130. const num2 = parseFloat(matches[3]);
  131.  
  132. switch (operation) {
  133. case "+":
  134. result = num1 + num2;
  135. break;
  136. case "-":
  137. result = num1 - num2;
  138. break;
  139. case "x":
  140. case "*":
  141. result = num1 * num2;
  142. break;
  143. case "divided by":
  144. case "/":
  145. if (num2 !== 0) {
  146. result = num1 / num2;
  147. } else {
  148. alert("Cannot divide by zero.");
  149. return;
  150. }
  151. break;
  152. default:
  153. alert("Could not understand the operation.");
  154. return;
  155. }
  156. } else {
  157. alert("Could not understand the word problem.");
  158. return;
  159. }
  160. }
  161.  
  162. alert(`The result of the problem is: ${result}`);
  163. }
  164.  
  165. // Run the calculator
  166. wordProblemCalculator();
  167. })();