Skip to content

Commit c429b82

Browse files
Labs: Make "give up" require a certain number of seconds (#729)
To encourage people to *try* to do the labs, if they pick "give up" too soon after loading, we'll instead tell them to try harder first. It also shows the number of seconds elapsed (a strong hint that this is timed). This intentionally does not show the minimum time. The purpose is to get people to work on the lab, not to encourage them to game the timer. Signed-off-by: David A. Wheeler <[email protected]>
1 parent a04d174 commit c429b82

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

docs/labs/checker.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ let page_definitions = {}; // Definitions used when preprocessing regexes
1515
let user_solved = false; // True if user has *ever* solved it on this load
1616
let user_gave_up = false; // True if user ever gave up before user solved it
1717

18+
let startTime = Date.now();
19+
1820
// This array contains the default pattern preprocessing commands, in order.
1921
// We process every pattern through these (in order) to create a final regex
2022
// to be used to match a pattern.
@@ -394,6 +396,19 @@ function showAnswer(e) {
394396
alert(`We were expecting an answer like this:\n${goodAnswer}`);
395397
}
396398

399+
// "Give up" only shows the answer after this many seconds have elapsed.
400+
const MIN_DELAY_TIME = 60;
401+
402+
function maybeShowAnswer(e) {
403+
let currentTime = Date.now();
404+
let elapsedTime = (currentTime - startTime) / 1000; // in seconds
405+
if (elapsedTime < MIN_DELAY_TIME) {
406+
alert("Try harder! Don't give up so soon. Current time spent (in seconds): " + elapsedTime);
407+
} else {
408+
showAnswer(e);
409+
}
410+
}
411+
397412
/**
398413
* Reset form.
399414
* We have to implement this in JavaScript to ensure that the final
@@ -668,7 +683,7 @@ function initPage() {
668683
}
669684
}
670685
for (let giveUpButton of document.querySelectorAll("button.giveUpButton")) {
671-
giveUpButton.addEventListener('click', (e) => { showAnswer(e); });
686+
giveUpButton.addEventListener('click', (e) => { maybeShowAnswer(e); });
672687
if (!giveUpButton.title) {
673688
giveUpButton.title = 'Give up and show an answer.';
674689
}

0 commit comments

Comments
 (0)