This code implements a bot for automating answering math questions...

July 3, 2025 at 10:00 PM

/** * FreeRiceBot Javascript Function * Author: Nathan Tsai * Last Updated: Apr 2022 */ function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } /** * FreeRiceBot function to automatically answer freerice.com questions until: * a specified amount of hours has passed or * a specified amount of rice was earned, * but not both. * * Currently only works for Multiplication Table category of questions. */ function freericebot() { // one liner for multiplication table answer // $(("div[data-target=a" + eval($("div[class=card-title]").textContent.replace(" x ", "*")).toString())).click() // all possible answers // document.querySelectorAll("div.card-button") /*** SET THIS VARIABLE BEFORE RUNNING THE BOT ***/ this.riceWanted = 30; // amount of rice wanted this.timeout = 0; this.riceDonated = parseInt(document.querySelector("div[class=rice-counter__value] span").textContent.replace(",", "")); this.maxRice = this.riceDonated + this.riceWanted; // rice total desired this.category = 0; FRBthis = this; this.run = async function() { this.category = document.querySelector("div[class=rice-counter__category-text]").textContent; console.log("have: " + this.riceDonated + "\n"); console.log("want: " + this.maxRice + "\n"); // run bot with 6-8 second intervals until max rice reached while(true) { ret = FRBthis.getRice(); if (ret != 0) { break; } delay = 6000 + Math.random() * 2000; await sleep(delay); } } this.getRice = function() { this.riceDonated = parseInt(document.querySelector("div[class=rice-counter__value] span").textContent.replace(",", "")); console.log("earned: " + this.riceDonated + " / " + this.maxRice + "\n"); if (this.riceDonated >= this.maxRice) { console.log("stopping bot"); return 1; } question = document.getElementsByClassName("card-title")[0].textContent; answer = 0; if (this.category == "Multiplication Table") { question = question.replace("x", "*"); answer = eval(question); } // TODO: handle fraction questions and fraction eval to answer string to target button else if (this.category == "Basic Math (Pre-Algebra)") { // Handle rounding questions if (question.includes("rounded")) { question = question.replace(" rounded =", ""); answer = Math.round(parseInt(question)); } // Handle multiplication questions else if (question.includes("x")) { question = question.replace("x", "*"); question = question.replace(" =", ""); answer = eval(question); } // Handle fraction questions else if (question.includes("")) { } // Handle other operation questions else { question = question.replace(" =", ""); answer = eval(question); } } // Find target button element button = [...document.querySelectorAll("div.card-button")].filter(div => div.innerText == (answer).toString())[0] button.click(); return 0; } } var bot = new freericebot(); bot.run();

This code implements a bot for automating answering math questions on the website freerice.com, which is a quiz-based game where users answer questions to earn "grains of rice" that are donated to charity. Here's what the code does in detail:

Key Function Breakdown:

  1. sleep(ms):

    • A helper function that delays execution for a specified number of milliseconds. It uses a Promise to pause execution in an asynchronous context.
  2. freericebot (Main Function):

    • This defines a bot with functionalities to:
      • Automatically answer questions in the "Multiplication Table" or "Basic Math (Pre-Algebra)" categories on freerice.com.
      • Stop running after earning a specified amount of rice (controlled by the variable riceWanted).

    How the bot works:

    • Initialization:

      • riceWanted: Number of additional rice grains the bot should aim to earn.
      • riceDonated: Tracks the current amount of rice donated (read from the website).
      • maxRice: The total target rice amount (sum of riceDonated and riceWanted).
    • Main Loop: (this.run):

      • The bot checks the category of the current quiz (e.g., "Multiplication Table"), and logs the current and target rice amounts.
      • It periodically calls getRice to answer a question.
      • The bot waits for a random interval (6-8 seconds) between answering questions to mimic human activity.
    • Answering Questions: (this.getRice):

      • Parses the current question displayed on the freerice.com interface (e.g., multiplication expressions like "6 x 8" or math questions like "12 + 3").
      • Calculates the correct answer using JavaScript's eval() function or other math operations (Math.round, etc.).
      • Searches for the HTML element corresponding to the correct answer on the page (div.card-button), and simulates a click on the appropriate button.
      • Terminates and logs a message if the target rice amount (maxRice) has been reached.
  3. Automation:

    • Upon declaring and running var bot = new freericebot();, the bot begins operating and continues until the target amount of rice is achieved.

Specific Features:

  • Categories Supported:

    • Primarily supports answering Multiplication Table questions ("6 x 3" → calculates 18).
    • Implements partial handling for basic pre-algebra questions (e.g., rounding, basic arithmetic).
  • Simulating Human Behavior:

    • Introduces random delays (6 to 8 seconds) between question attempts using sleep to avoid suspicion of bot activity.
  • Stopping Condition:

    • Stops if the maxRice target is achieved.

Limitations:

  • Category Limitations:

    • Explicitly tailored for answering "Multiplication Table" and simple "Basic Math (Pre-Algebra)" questions.
    • Doesn't fully support complex fraction questions, which are noted as a TODO in the code.
  • Reliance on Page Structure:

    • The bot queries specific HTML elements (div.card-button, div.card-title) and their attributes. If freerice.com changes its DOM structure, the bot might require updates.
  • Potential Risks:

    • Using automation (like this bot) may violate freerice.com's terms of service.
    • The use of eval() for evaluating math expressions has some security implications, though it seems restrained to trusted inputs in this context.

Overall:

The code is a simple and targeted bot for earning "rice grains" on freerice.com by automating the answering process for specific math categories. It programmatically interacts with the website by parsing questions, calculating answers, finding correct buttons, and simulating clicks.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node