Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@
(Math.floor(selected.r/3)===Math.floor(r/3) && Math.floor(selected.c/3)===Math.floor(c/3));
}

function normalizeElapsed(value){
const parsed=Number(value);
if(!Number.isFinite(parsed) || parsed<0) return 0;
return Math.floor(parsed);
}

function formatTime(t){
return `${String(Math.floor(t/60)).padStart(2,'0')}:${String(t%60).padStart(2,'0')}`;
const normalized=normalizeElapsed(t);
return `${String(Math.floor(normalized/60)).padStart(2,'0')}:${String(normalized%60).padStart(2,'0')}`;
}

// ── Notes ────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -566,7 +573,7 @@
startingGrid=data.startingGrid||data.grid.map(row=>row.slice());
notes=data.notes.map(row=>row.map(arr=>new Set(arr)));
selected=data.selected;
elapsed=data.elapsed||0;
elapsed=normalizeElapsed(data.elapsed ?? data.timeElapsed ?? data.timer ?? data.timeSeconds);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic for retrieving the elapsed time by checking multiple legacy keys is also used on line 956. To improve maintainability and avoid future inconsistencies where one location is updated but the other is not, consider extracting this logic into a dedicated helper function.

For example, you could create a function:

function getPersistedElapsedTime(data) {
  return data.elapsed ?? data.timeElapsed ?? data.timer ?? data.timeSeconds;
}

And then use it here and on line 956:

// line 576
elapsed = normalizeElapsed(getPersistedElapsedTime(data));

// line 956
timeStat.textContent = formatTime(getPersistedElapsedTime(saved));

This would make the code more DRY (Don't Repeat Yourself) and easier to maintain.

notesMode=!!data.notesMode;
autoCleanup=data.autoCleanup!==false;
difficultyEl.value=data.difficulty||'medium';
Expand Down Expand Up @@ -946,7 +953,7 @@
try{
const saved=JSON.parse(raw);
const modal=document.getElementById('resumeModal');
timeStat.textContent=formatTime(Number(saved.elapsed) || 0);
timeStat.textContent=formatTime(saved.elapsed ?? saved.timeElapsed ?? saved.timer ?? saved.timeSeconds);
modal.hidden=false;
document.getElementById('resumeYesBtn').onclick=()=>{ modal.hidden=true; applyLoadedData(saved); };
document.getElementById('resumeNoBtn').onclick=()=>{ modal.hidden=true; newGame(); };
Expand Down
Loading