feat: Implement SM2

This commit is contained in:
Alexander Polynomdivision 2018-09-16 12:21:08 +02:00
parent 1c244d6f5d
commit 1dfdd2f409
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,35 @@
import { dayInNDays } from "../../utils/date";
export interface ISchedulingData {
easiness: number;
consecutiveCorrectAnswers: number;
nextDueDate: number;
};
export enum AnswerType {
CORRECT,
WRONG,
};
function performanceRating(answer: AnswerType): number {
switch (answer) {
case AnswerType.WRONG:
return 1;
case AnswerType.CORRECT:
return 4;
}
}
export function updateSchedulingData(data: ISchedulingData, answer: AnswerType): ISchedulingData {
const perfRating = performanceRating(answer);
data.easiness += -0.8 + 0.28 * perfRating + 0.02 * Math.pow(perfRating, 2);
data.consecutiveCorrectAnswers = answer === AnswerType.CORRECT ? (
data.consecutiveCorrectAnswers + 1
) : 0;
data.nextDueDate = answer === AnswerType.CORRECT ? (
dayInNDays(6 * Math.pow(data.easiness, data.consecutiveCorrectAnswers - 1))
) : dayInNDays(1);
return data;
}

9
src/utils/date.ts Normal file
View File

@ -0,0 +1,9 @@
export const DAY_IN_MILLI = 24 * 60 * 60 * 1000;
export function daysToMilli(n: number): number {
return DAY_IN_MILLI * n;
};
export function dayInNDays(n: number): number {
return Date.now() * daysToMilli(n);
}