01-edu / public

📚 @01-edu's Public Repository
http://public.01-edu.org/
199 stars 425 forks source link

throttle subject #2499

Closed veron-ee closed 3 months ago

veron-ee commented 3 months ago

throttle

The tests are passed, but shows as a fail

Screenshot 2024-03-17 at 00 52 28
zanninso commented 3 months ago

Hello @veron-ee Can you share you solution? And try to change anything in your code lik adding a space then commit it and see if it will pass or not!

veron-ee commented 3 months ago

Hello! The problem indeed went away with the new commit. The solution looks like this:

/*
Create two functions that will work like _.throttle from lodash.

throttle: don't worry about the options.
opThrottle: implement the trailing and leading options.

lisakommentaar uue commiti jaoks
*/

function throttle(func, wait) {
    let execution = 0;
    return function () {
        const now = +new Date();
        if (now - execution > wait) {
            func.apply(this, arguments);
            execution = now;
        }
    };
}

function opThrottle(func, wait, { leading = false, trailing = true } = {}) {
    let execution = 0;
    let timer = null;

    return function () {
        const now = +new Date();
        if (!execution && leading === false) {
            execution = now;
        }
        if (now - execution > wait) {
            if (timer) {
                clearTimeout(timer);
                timer = null;
            }
            func.apply(this, arguments);
            execution = now;
        } else if (!timer && trailing !== false) {
            timer = setTimeout(() => {
                func.apply(this, arguments);
                execution = +new Date();
                timer = null;
            }, wait);
        }
    };
}
zanninso commented 3 months ago

Hello @veron-ee,

Your code appears to be correct, and the tests have passed successfully. This indicates that the issue was not with the tests but rather with the platform. Somehow, it was unable to execute the tests for the previous commit, leading to an erroneous indication of a problem in the code.

Since there is no need to make any changes to the tests, I will proceed to close this issue now that the problem has been resolved.