Skip to content
Classroom

Feedback Macros

Build dynamic, conditional feedback messages based on student responses.

IMathAS question code is PHP-like: use $var = value;, if (condition) { ... }, and standard operators. Feedback is assigned by setting the feedback variable inside the Answer section. There are no @-prefixed Blade directives in IMathAS — if you see @php, @if, or @endif in a question, it will not work.

Feedback Helpers

wrongans($msg)

Display a message when the student submits a wrong answer. Add it to the Answer section after the answer definition.

[number]
answer = 42;
wrongans("Not quite. Double-check your arithmetic and try again.");

showwork($label)

Add a text input where students can show their work alongside their answer.

[number]
answer = 42;
showwork("Show the steps you used to get your answer:");

The label is the prompt shown above the work-input box. The work is saved with the student's submission for instructor review.

ifansunit($unit, $msg)

Give targeted feedback when the student's answer is numerically correct but uses the wrong units (or when units are missing).

[number]
answer = 9.8;
ifansunit("ft/s^2", "Close — but this question expects SI units (m/s^2).");

Note

Exact signatures for unit-aware helpers vary across IMathAS versions. If ifansunit isn't available on your instance, use a manual if check against the unit string parsed from the student's answer.

Basic Feedback

feedback Variable

Assign feedback text in the Answer section:

[number]
answer = 42;

feedback = "Correct! 42 is the answer to life, the universe, and everything.";

Conditional Feedback

Use if statements to provide different messages based on responses:

$correctAnswer = 25;

[number]
answer = $correctAnswer;
tolerance = 0;

if ($studentAnswer == $correctAnswer) {
    feedback = "Perfect!";
} else {
    feedback = "Incorrect. Try again.";
}

Feedback Based on Magnitude of Error

$answer = 10;

[number]
answer = $answer;
tolerance = 0.01;

$error = abs($studentAnswer - $answer);

if ($studentAnswer == $answer) {
    feedback = "Exactly right!";
} elseif ($error < 0.5) {
    feedback = "Very close! Check your rounding.";
} elseif ($error < 2) {
    feedback = "In the ballpark. Review your calculation.";
} elseif ($error < 5) {
    feedback = "You're on the right track, but off by quite a bit.";
} else {
    feedback = "Not quite. Start from the beginning.";
}

Feedback for Common Mistakes

$correct = 20;

[number]
answer = $correct;

$commonErrors = array(
    10 => "Did you forget to multiply by 2?",
    30 => "Did you add instead of subtract?",
    40 => "Close! But check your division step.",
    100 => "That's 5 times too large.",
    0 => "You may have made an arithmetic error. Try again.",
);

if (isset($commonErrors[$studentAnswer])) {
    feedback = $commonErrors[$studentAnswer];
} else {
    feedback = "Incorrect. Review the problem and try again.";
}

Feedback with Multiple Parts

[number]
answer = 15;

$hints = array();

if ($studentAnswer < 10) {
    $hints[] = "Your answer is too small.";
}

if ($studentAnswer > 20) {
    $hints[] = "Your answer is too large.";
}

if ($studentAnswer % 5 != 0) {
    $hints[] = "The correct answer is a multiple of 5.";
}

if (count($hints) > 0) {
    feedback = implode(" ", $hints);
} else {
    feedback = "Close! Check your work step-by-step.";
}

Feedback for Algebraic Expressions

[calculated]
answer = x^2 + 2*x + 1;
simplify = full;

if ($studentAnswer == "x^2 + 2*x + 1") {
    feedback = "Correct! That's the expanded form.";
} elseif ($studentAnswer == "(x+1)^2") {
    feedback = "Also correct! That's the factored form.";
} else {
    feedback = "Your expression doesn't match. Expand (x+1)^2 to verify.";
}

Formatted Feedback

Use HTML formatting in feedback (allowed in most contexts):

[number]
answer = 100;

if ($studentAnswer == 100) {
    feedback = "<strong>Perfect!</strong> You got it right.";
} else {
    feedback = "<em>Not quite.</em> The answer is 100.";
}

Feedback with Mathematical Display

Use backticks for math in feedback:

[number]
answer = 10;

if ($studentAnswer != 10) {
    feedback = "Not correct. The answer to `2x + 3 = 23` is `x = 10`.";
}

Common Feedback Patterns

Numeric Threshold

if ($studentAnswer >= 90) {
    feedback = "Excellent!";
} elseif ($studentAnswer >= 70) {
    feedback = "Good, but review the harder cases.";
} else {
    feedback = "More practice needed.";
}

Multiple Correct Answers

$correctAnswers = array("Paris", "London", "Berlin");

if (in_array($studentAnswer, $correctAnswers)) {
    feedback = "Correct! That's a major European capital.";
}

Tips

  • Be encouraging: Positive feedback helps learning
  • Provide hints, not answers: Guide students toward understanding
  • Be specific: Generic "try again" is less helpful than "Check your arithmetic in step 2"
  • Use mathematical notation: Display equations using backticks
  • Format for readability: Break long feedback into sentences or bullets
  • Test your conditions: Verify all branches work correctly
  • No Blade directives: IMathAS does not use @php, @if, @endif, etc. Use plain PHP-like syntax

See Also