slaypni / SM-15

Spaced repetition for memorizing tons of things.
MIT License
158 stars 34 forks source link

OptimumInterval output is NaN value. #6

Open my-yy opened 3 years ago

my-yy commented 3 years ago

I want to use this project as a js library, but I found the output optimumInterval sometimes can be a NaN value: When I put three review histories into it: mark=5,date=now mark=3,date=now+10days mark=3,date=now+100days

the final optimumInterval will be a NaN value:

repetition:0,date:2021-01-01,mark:5,optimumInterval:4.0 hour

repetition:1,date:2021-01-11,mark:3,optimumInterval:59.2 days

repetition:2,date:2021-04-11,mark:3,optimumInterval:NaN days

code to reproduce this issue:


const sm_util = require("./sm.js")
const sm = new sm_util.SM()
const ONE_DAY = 24 * 3600 * 1000

function mytest() {
    const now = new Date().getTime()

    const item = sm.addItem("")

    answer_core(5, item, new Date(now))

    answer_core(3, item, new Date(now + ONE_DAY * 10))

    answer_core(3, item, new Date(now + ONE_DAY * 100))
}

function answer_core(mark, item, date) {
    sm.answer(mark, item, date)
    const data = item.data()
    const optimumInterval = format_interval(data["optimumInterval"])
    const repetition = data["repetition"]
    console.log(`repetition:${repetition},date:${formatDate(date)},mark:${mark},optimumInterval:${optimumInterval}`)
}

function format_interval(optimumInterval) {
    let days = optimumInterval / 24 / 3600 / 1000
    if (days < 1.0) {
        days = (optimumInterval / 1000 / 3600).toFixed(1) + " hour"
    } else {
        days = days.toFixed(1) + " days"
    }
    return days
}

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2)
        month = '0' + month;
    if (day.length < 2)
        day = '0' + day;

    return [year, month, day].join('-');
}

mytest()