supermemo
Version:
A JavaScript/TypeScript implementation of the SuperMemo 2 (SM2) algorithm for spaced based repetition flashcards.
47 lines (46 loc) • 1.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.supermemo = supermemo;
/**
* Applies the SuperMemo 2 (SM-2) algorithm to update an item's learning parameters
* based on a given recall grade.
*
* @param item - The current SuperMemo item containing interval, repetition, and ease factor.
* @param grade - The grade (0-5) assigned to the recall attempt.
* @returns The updated SuperMemo item with adjusted interval, repetition, and ease factor.
*/
function supermemo(item, grade) {
let nextInterval;
let nextRepetition;
let nextEfactor;
if (grade >= 3) {
if (item.repetition === 0) {
nextInterval = 1;
nextRepetition = 1;
}
else if (item.repetition === 1) {
nextInterval = 6;
nextRepetition = 2;
}
else {
nextInterval = Math.round(item.interval * item.efactor);
nextRepetition = item.repetition + 1;
}
}
else {
nextInterval = 1;
nextRepetition = 0;
}
nextEfactor = item.efactor + (0.1 - (5 - grade) * (0.08 + (5 - grade) * 0.02));
if (nextEfactor < 1.3)
nextEfactor = 1.3;
return {
interval: nextInterval,
repetition: nextRepetition,
efactor: nextEfactor,
};
}
/**
* Default export of the SuperMemo function.
*/
exports.default = supermemo;