Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only special case empty Hanzi Math results of subtraction #47

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions public/js/modules/hanzi-math.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function evaluate(input) {
}
let leftOperandList = null;
let nextOperator = null;
let previousOperation = null;
for (const character of input) {
// no left side yet? set it and move on.
// note that leftOperandList can later become empty, which is then handled in the plus operation
Expand All @@ -122,6 +123,9 @@ function evaluate(input) {
}
// it's an operator, so note that as our next operation and move on.
if (operators.has(character)) {
if (nextOperator !== null) {
previousOperation = nextOperator;
}
nextOperator = character;
continue;
}
Expand Down Expand Up @@ -155,16 +159,16 @@ function evaluate(input) {
}
}
} else {
// Nothing there now, due to subtraction to 0 or due to not finding results of an add?
// Add the right operand and return its compounds...
// TODO: this does mean adds of 3 or more items can be weird, with the first addition going to []
// Nothing there now, due to subtraction to [] or due to not finding results of an add.
// Add the right operand and return its compounds, but first
// check for previous subtraction before special casing the empty list.
// Otherwise, adds of 3 or more items can be weird, with the first addition going to []
// and then the next addition adding items that probably shouldn't be there
// example:
// 我+你-->no result
// 我+你+我-->results as though it was just 我.
// Should probably only do special empty handling
// if the prior operation was a subtraction...
if (leftOperandList.length < 1) {
// The `previousOperation` check avoids that.
if (previousOperation === '-' && leftOperandList.length < 1) {
leftOperandList.push(character);
}
// if it's addition, find transitive compounds as the set of candidates,
Expand Down
Loading