 # Help with self-checking Pythagorean Theorem

On slide 2, I want students to type in 6^2+8^2=10^2 and have it say “correct” for them.

1 Like

This is a little lengthy, but it accomplishes two tasks. The `correct` variable checks to see if the expression matches what you are looking for. Then I used some of the new pattern matching to see if its in a^2 + b^2 = c^2 format. I can see a student possibly entering 36+64=100 and being confused as to why it was wrong. The pattern is used in an errorMessage to guide the student toward what you are looking for. This code can go in your math input component.

``````equation = parseEquation(this.latex)
lhs = numericValue(equation.lhs)
rhs = numericValue(equation.rhs)

correct = lhs=rhs and countNumberUsage(this.latex)=6 and  countNumberUsage(this.latex,2)=3 and countNumberUsage(this.latex,6)=1 and countNumberUsage(this.latex,8)=1 and countNumberUsage(this.latex,10)=1
correct: correct

p = patterns

equationlhs = parseEquation(this.latex).lhs
equationrhs = parseEquation(this.latex).rhs

matchLeft = p.sum(p.exponent(p.integer,p.integer),p.exponent(p.integer,p.integer)).strictOrder
matchRight = p.exponent(p.integer,p.integer)

leftMatches = when matchLeft.matches(equationlhs) "Yes" otherwise "No"
rightMatches = when matchRight.matches(equationrhs) "Yes" otherwise "No"

errorMessage: when not(matchLeft.matches(equationlhs) or matchRight.matches(equationrhs)) "Write in a^2 + b^2 = c^2 form." otherwise ""
``````

If you want to provide some feedback in a note, use this code. In the actual note, you should type `\${feedback}`

``````feedback = when input.script.correct and input.submitted "Correct" when input.submitted "Incorrect" otherwise ""
``````
1 Like

Building on that, I was playing around to see if you can just use the patterns to check the EXACT numbers. Looks like you can, by using `p.literal`

``````p = patterns

equationlhs = parseEquation(this.latex).lhs
equationrhs = parseEquation(this.latex).rhs

matchLeft = p.sum(p.exponent(p.literal("6"),p.literal("2")),p.exponent(p.literal("8"),p.literal("2"))).strictOrder
matchRight = p.exponent(p.literal("10"),p.literal("2"))

leftMatches = when matchLeft.matches(equationlhs) "Yes" otherwise "No"
rightMatches = when matchRight.matches(equationrhs) "Yes" otherwise "No"

correct = matchLeft.matches(equationlhs) and matchRight.matches(equationrhs)

correct: correct

errorMessage: when not(matchLeft.matches(equationlhs) or matchRight.matches(equationrhs)) "Write in a^2 + b^2 = c^2 form." otherwise ""
``````
1 Like

The literal pattern will actually check exact characters, not numbers. If you want to check for exact numbers you can use the .satisfies member. E.g. patterns.number.sarisfies(“x=6”) checks for a number that equals 6.

3 Likes

Thanks! This all helped!

1 Like