I am creating an activity on adding and subtracting fractions. I am using random numbers so that students have different problems. I am using a hidden graph to get the answer so that students will have to simplify their final answer. I am trying to figure out how to parse the answer if the answer is negative and the student puts the negative in front of the fraction. I tried reading in the numerator as an expression instead of an integer, but that didn’t work. I know patterns.negative exists, but I don’t know how to use it or if it would even help here.

Do you know how to use p.anyOf()?

Make one pattern for a fraction, call it frac.

Then make another pattern negFrac = p.negative(frac)

Then you can have a final pattern like:

validFraction = p.anyOf(frac, negFrac)

Here is an example: Fraction checking with pattern matching • Activity Builder by Desmos

This makes sense! Thank you.

Follow up question: I want to have them simplify fractions so I’ve been using a hidden graph to do the calculations. So I figure out what the simplified numerator should be and what the simplified denominator should be and then parse the input so I can compare the numerators and the denominators. Is there a way to still do something like this while giving students the flexibility on where they put the negative symbol?

Yes, you can. I think that’s a bit harder because you have to not just make sure it has the right value as a fraction, but you have to check that the specific num and denom are correct. I added a second screen that I think does what you are asking.

Basically, you take the student input and check if it meets either the regular or negated fraction patterns, and then pull out the numerator and denominator. Then, check if the overall fraction comes out positive or negative.

Sink these 3 values (numerator, denominator, and whether it is negative or not) into the graph and have it compare against the actual values.