리펙토링: 코드 최적화

This commit is contained in:
2026-05-31 16:21:55 +09:00
parent 6300fe1082
commit de0a92d10f
11 changed files with 130 additions and 156 deletions
+8 -31
View File
@@ -3,6 +3,7 @@ import type {
ParenthesisSignProblem,
UserAnswer,
} from "./types";
import { formatParenthesisExpressionLatex } from "./parenthesis-latex";
const MIN_MULTIPLIER = -9;
const MAX_MULTIPLIER = -1;
@@ -33,19 +34,6 @@ function formatLatexTerms(terms: number[]) {
.join(" ");
}
function formatParenthesisTerms(
terms: number[],
operators: ("+" | "-")[],
) {
return terms
.map((term, index) => {
if (index === 0) return `${term}`;
return `${operators[index - 1]} ${term}`;
})
.join(" ");
}
function createProblem({
multiplier,
terms,
@@ -57,9 +45,6 @@ function createProblem({
return operators[index - 1] === "-" ? -term : term;
});
const distributedTerms = signedTerms.map((term) => multiplier * term);
const [a, b] = terms;
const [operator] = operators;
const [distributedA, distributedB] = distributedTerms;
const key = [
multiplier,
...terms.flatMap((term, index) =>
@@ -73,16 +58,13 @@ function createProblem({
multiplier,
terms,
operators,
a,
operator,
b,
distributedTerms,
distributedA,
distributedB,
latexBefore: `${multiplier}\\left(${formatParenthesisTerms(
latexBefore: formatParenthesisExpressionLatex({
multiplier,
terms,
operators,
)}\\right)`,
formatTermLatex: (term) => `${term}`,
}),
latexAfter: formatLatexTerms(distributedTerms),
};
}
@@ -222,16 +204,11 @@ export function gradeAnswer(
problem: ParenthesisSignProblem,
answer: UserAnswer,
): GradeResult {
const userAnswer =
answer.terms ?? (answer.a !== undefined && answer.b !== undefined
? [answer.a, answer.b]
: []);
return {
correct:
userAnswer.length === problem.distributedTerms.length &&
userAnswer.every((value, index) => value === problem.distributedTerms[index]),
userAnswer,
answer.terms.length === problem.distributedTerms.length &&
answer.terms.every((value, index) => value === problem.distributedTerms[index]),
userAnswer: answer.terms,
correctAnswer: problem.distributedTerms,
};
}