Problem with Patterns

I have the following code in my computational layer of a math response box. The lines that are not commented out currenly work, but don’t account for when the answer has an exponent of 0 and the student doesn’t enter the exponent (which is OK). So I attempted to fix that by entering commenting out the lines that are currently working and changing them to the lines commented out here. When I do that I get an error when entering a value with and exponent component. Even when my warning is commented out also. The message is that it cannont evaluate the entry. My project is [here]
(High School Placement Test Practice • Activity Builder by Desmos Classroom) The problem child is screen 7.

set up the pattern required

pattern = p.product(p.number,p.exponent(p.literal(“x”),p.integer))
matches = pattern.matches(this.latex)
warning: when not(matches) “Enter answer as Coefficient with variable x and exponent on the x variable.” otherwise “”

set up the pattern required REPLACED ABOVE 3 STATEMENTS WITH 6 STATEMENTS BELOW

#patternE = p.product(p.number,p.exponent(p.literal(“x”),p.integer))
#patternNoE = p.integer
#matchE = patternE.matches(this.latex)
#matchNoE = patternNoE.matches(this.latex)
#matches = matchE or matchNoE
#warning: when not(matches) “Enter answer as Coefficient with variable x and exponent on the x variable.” otherwise “”

separate the expression parts

coefficient = pattern.parse(this.latex).term1.numericValue
exponent = numericValue(pattern.parse(this.latex).term2.exponent.latex)

separate the expression parts REPLACED ABOVE 2 STATEMENTS WITH 2 STATEMENTS BELOW

#coefficient = when matchNoE this.numericValue otherwise patternE.parse(this.latex).term1.numericValue
#exponent = when matchNoE 0 otherwise numericValue(patternE.parse(this.latex).term2.exponent.latex)

I think one approach you might want is to define the different potential patterns that you are willing to accept and use the anyOf() pattern to verify the latex. Then, use a set of when statements to parse out the values based on the specific pattern that actually matched. I edited the middle part of the CL on slide 7 as an example. [Copy of] High School Placement Test Practice • Activity Builder by Desmos Classroom

Also, note that there is a formatLatex() function that does some basic clean-up of latex strings, removing 1s from coefficients and powers, changing + -3 to just -3, etc. This makes it a lot easier to generate nice looking random expressions.

Thanks. I’m just starting with computational layer stuff and desmos in general and this really helps.

No problem, happy to help. Once you’re working with patterns and randomization, you’re definitely in the deep end of the CL pool!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.