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

Add "ALL" Option for Randomized Operation Selection #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions components/Problem.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ export default function Problem({
operation,
operands,
maxAnswerLength,
answerString
answerString,
operationFromOperands
}) {
const operator = OPERATORS[operation];
const operator = OPERATORS[operationFromOperands];
const answerWidthClass = ANSWER_WIDTHS[maxAnswerLength];
let textSizeClass;
if (maxAnswerLength <= 8) {
Expand Down
4 changes: 3 additions & 1 deletion components/Set.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default function Set({ solvedProblems, setSolvedProblems, onSetEnd }) {
setStartTime,
problemStartTime,
maxAnswerLength,
handleKeypadPress
handleKeypadPress,
operationFromOperands
} = useSet(
solvedProblems,
setSolvedProblems,
Expand Down Expand Up @@ -69,6 +70,7 @@ export default function Set({ solvedProblems, setSolvedProblems, onSetEnd }) {
<div className={'flex flex-auto items-center'}>
<Problem
operation={operation}
operationFromOperands={operationFromOperands}
operands={operands}
maxAnswerLength={maxAnswerLength}
answerString={answerString}
Expand Down
4 changes: 3 additions & 1 deletion components/SetResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ export default function SetResults({ problems }) {
<div className='max-h-[9.5rem] w-full overflow-auto scroll-smooth sm:max-h-[22.5rem]'>
<div className='grid grid-cols-[auto_auto] gap-y-0.5 gap-x-2.5'>
{problems.map((problem, i) => {
const { operation, operands, centiseconds } = problem;
const { centiseconds } = problem;
let operands = problem.operands.operands;
let operation = problem.operands.operation;
const operator = OPERATORS[operation];
const refProp =
selectedBest && i === selectedBest.startIndex
Expand Down
124 changes: 70 additions & 54 deletions hooks/useSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ export default function useSet(

useEffect(() => {
let correctAnswer;
switch (operation) {
let operand = operands.operands;
switch (operands.operation) {
case 'ADDITION':
correctAnswer = BigInt(operands[0]) + BigInt(operands[1]);
correctAnswer = BigInt(operand[0]) + BigInt(operand[1]);
break;
case 'SUBTRACTION':
correctAnswer = BigInt(operands[0]) - BigInt(operands[1]);
correctAnswer = BigInt(operand[0]) - BigInt(operand[1]);
break;
case 'MULTIPLICATION':
correctAnswer = BigInt(operands[0]) * BigInt(operands[1]);
correctAnswer = BigInt(operand[0]) * BigInt(operand[1]);
break;
case 'DIVISION':
correctAnswer = BigInt(operands[0]) / BigInt(operands[1]);
correctAnswer = BigInt(operand[0]) / BigInt(operand[1]);
break;
}
if (BigInt(answerString) === correctAnswer) {
Expand Down Expand Up @@ -125,9 +126,9 @@ export default function useSet(
onSetEnd();
}
}, [solvedProblems, setProblemCount, onSetEnd]);

return {
operands,
operands: operands.operands,
operationFromOperands: operands.operation,
answerString,
setStartTime,
problemStartTime,
Expand All @@ -137,8 +138,10 @@ export default function useSet(
}

function getMaxAnswerLength(operands, operation) {
const operandLengths = operands.map((operand) => operand.toString().length);
switch (operation) {
const operandLengths = operands.operands.map(
(operand) => operand.toString().length
);
switch (operands.operation) {
case 'ADDITION':
return Math.max(...operandLengths) + 1;
case 'SUBTRACTION':
Expand All @@ -161,55 +164,68 @@ function getRandomIntegerByLength(length) {
}

function getOperands(operation, operandLengths) {
const generateAdditionOrMultiplication = () =>
operandLengths.map((length) =>
length === 1 ? getRandomInteger(2, 10) : getRandomIntegerByLength(length)
);

const generateSubtraction = () => {
if (operandLengths[0] !== operandLengths[1]) {
return operandLengths.map((length) => getRandomIntegerByLength(length));
}
const minMinuend = Math.pow(10, operandLengths[0] - 1) + 1;
const maxMinuend = Math.pow(10, operandLengths[0]);
const minuend = getRandomInteger(minMinuend, maxMinuend);
const subtrahend = getRandomInteger(minMinuend - 1, minuend);
return [minuend, subtrahend];
};

const generateDivision = () => {
let divisor, minQuotient, maxQuotient;
if (operandLengths[0] === operandLengths[1]) {
divisor =
operandLengths[0] === 1
? getRandomInteger(2, 5)
: getRandomInteger(
Math.pow(10, operandLengths[0] - 1),
Math.pow(10, operandLengths[0]) / 2
);
minQuotient = 2;
maxQuotient = Math.floor((Math.pow(10, operandLengths[0]) - 1) / divisor);
} else {
divisor =
operandLengths[1] === 1
? getRandomInteger(2, 10)
: getRandomIntegerByLength(operandLengths[1]);
minQuotient = Math.ceil(Math.pow(10, operandLengths[0] - 1) / divisor);
maxQuotient = Math.floor((Math.pow(10, operandLengths[0]) - 1) / divisor);
}
const quotient = getRandomInteger(minQuotient, maxQuotient + 1);
const dividend = divisor * quotient;
return [dividend, divisor];
};

const operations = ['ADDITION', 'MULTIPLICATION', 'SUBTRACTION', 'DIVISION'];

switch (operation) {
case 'ADDITION':
case 'MULTIPLICATION':
return operandLengths.map((length) =>
length === 1
? // Exclude 1.
getRandomInteger(2, 10)
: getRandomIntegerByLength(length)
);
return { operation, operands: generateAdditionOrMultiplication() };

case 'SUBTRACTION':
if (operandLengths[0] !== operandLengths[1]) {
return operandLengths.map((length) => getRandomIntegerByLength(length));
}
const minMinuend = Math.pow(10, operandLengths[0] - 1) + 1;
const maxMinuend = Math.pow(10, operandLengths[0]);
const minuend = getRandomInteger(minMinuend, maxMinuend);
const subtrahend = getRandomInteger(minMinuend - 1, minuend);
return [minuend, subtrahend];
return { operation, operands: generateSubtraction() };

case 'DIVISION':
let divisor, minQuotient, maxQuotient;
if (operandLengths[0] === operandLengths[1]) {
if (operandLengths[0] === 1) {
// Exclude 1.
divisor = getRandomInteger(2, 5);
} else {
const minDivisor = Math.pow(10, operandLengths[0] - 1);
const maxDivisor = Math.pow(10, operandLengths[0]) / 2;
divisor = getRandomInteger(minDivisor, maxDivisor);
}
minQuotient = 2;
// maxQuotient is inclusive.
maxQuotient = Math.floor(
(Math.pow(10, operandLengths[0]) - 1) / divisor
);
} else {
if (operandLengths[1] === 1) {
// Exclude 1.
divisor = getRandomInteger(2, 10);
} else {
divisor = getRandomIntegerByLength(operandLengths[1]);
}
minQuotient = Math.ceil(Math.pow(10, operandLengths[0] - 1) / divisor);
// maxQuotient is inclusive.
maxQuotient = Math.floor(
(Math.pow(10, operandLengths[0]) - 1) / divisor
);
}
const quotient = getRandomInteger(minQuotient, maxQuotient + 1);
const dividend = divisor * quotient;
return [dividend, divisor];
return { operation, operands: generateDivision() };

case 'ALL':
const randomOperation =
operations[Math.floor(Math.random() * operations.length)];
console.log(randomOperation, 'Randomly selected operation');
return getOperands(randomOperation, operandLengths);

default:
throw new Error('Unsupported operation');
}
}

3 changes: 2 additions & 1 deletion utils/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export const OPERATORS = {
ADDITION: '+',
SUBTRACTION: '−',
MULTIPLICATION: '×',
DIVISION: '÷'
DIVISION: '÷',
ALL: '○'
};

export function pluralize(word, count, nbsp = false) {
Expand Down