Comprehensive Word Problem Calculator

A versatile word problem calculator that handles various equations and unknowns

目前為 2024-09-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Comprehensive Word Problem Calculator
// @namespace    http://tampermonkey.net/
// @version      2.9
// @description  A versatile word problem calculator that handles various equations and unknowns
// @author       Your Name
// @match        https://easybridge-dashboard-web.savvaseasybridge.com/dashboard/student?ssoLogin=true
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function wordProblemCalculator() {
        const problem = prompt("Enter a word problem (e.g., '8 divided by 2' or '9 times what equals 81'). You can also type 'div' for division:").toLowerCase();
        let result;

        // Handle equal distribution phrases
        if (problem.includes("bananas") && problem.includes("to put in") && problem.includes("boxes")) {
            const numbers = problem.match(/\d+/g);
            if (numbers && numbers.length >= 2) {
                const total = parseFloat(numbers[0]); // Total number of bananas
                const boxes = parseFloat(numbers[1]); // Number of boxes
                result = total / boxes; // Calculate how many bananas per box
            } else {
                alert("Could not find enough numbers.");
                return;
            }
        } 
        // Handle division for "what"
        else if (problem.includes("times what") || problem.includes("what times") || problem.includes("what equals")) {
            const numbers = problem.match(/\d+/g);
            if (numbers && numbers.length === 2) {
                const num1 = parseFloat(numbers[0]); // The number before "times"
                const total = parseFloat(numbers[1]); // The number after "equals"
                result = total / num1; // Solve for "what"
            } else {
                alert("Could not find enough numbers.");
                return;
            }
        } 
        // Handle standard operations
        else {
            const matches = problem.match(/(\d+)\s*(\w+)\s*(\d+)/);
            if (matches) {
                const num1 = parseFloat(matches[1]);
                const operation = matches[2];
                const num2 = parseFloat(matches[3]);

                switch (operation) {
                    case "plus":
                    case "+":
                        result = num1 + num2;
                        break;
                    case "minus":
                    case "-":
                        result = num1 - num2;
                        break;
                    case "times":
                    case "x":
                        result = num1 * num2;
                        break;
                    case "div":
                    case "divided by":
                    case "/":
                        if (num2 !== 0) {
                            result = num1 / num2;
                        } else {
                            alert("Cannot divide by zero.");
                            return;
                        }
                        break;
                    default:
                        alert("Could not understand the operation.");
                        return;
                }
            } else {
                alert("Could not understand the word problem.");
                return;
            }
        }

        alert(`The result of the problem is: ${result}`);
    }

    // Run the calculator
    wordProblemCalculator();
})();