Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
<body class="state-green">
<main class="app" aria-live="polite">
<h1>Happy Button</h1>
<button id="happy-button" type="button" class="happy-button button-green">
Click me
</button>
<div class="button-row">
<button id="happy-button" type="button" class="happy-button button-green">
Click me
</button>
<button id="happy-button-2" type="button" class="happy-button button-red">
Click me too
</button>
</div>
<p id="click-status" class="status">Total clicks: 0</p>
<p id="celebration-status" class="status">Clicks until celebration: 5</p>
<p id="sequence-status" class="status">Sequence progress: 0 / 3</p>
<p id="celebration-message" class="celebration" hidden>
Congratulations! You clicked the button 5 times.
</p>
Expand Down
56 changes: 52 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const TOTAL_KEY = "happyButtonTotalClicks";
const CYCLE_KEY = "happyButtonCelebrationClicks";
const CELEBRATION_INTERVAL = 5;
const SEQUENCE_TARGET = 3;
const SEQUENCE_LENGTH = SEQUENCE_TARGET * 2;

const button = document.getElementById("happy-button");
const button2 = document.getElementById("happy-button-2");
const clickStatus = document.getElementById("click-status");
const celebrationStatus = document.getElementById("celebration-status");
const sequenceStatus = document.getElementById("sequence-status");
const celebrationMessage = document.getElementById("celebration-message");

function readStoredCount(key) {
Expand All @@ -14,6 +18,12 @@ function readStoredCount(key) {

let totalClicks = readStoredCount(TOTAL_KEY);
let celebrationClicks = readStoredCount(CYCLE_KEY);
let sequenceStep = 0;
let sequenceCelebrationActive = false;

function alternationsForStep(step) {
return Math.floor(step / 2);
}

function renderState(showCelebration = false) {
const isGreenButton = totalClicks % 2 === 0;
Expand All @@ -27,13 +37,41 @@ function renderState(showCelebration = false) {
celebrationStatus.textContent = `Clicks until celebration: ${
CELEBRATION_INTERVAL - celebrationClicks
}`;
if (showCelebration) {
sequenceStatus.textContent = `Sequence progress: ${alternationsForStep(
sequenceStep,
)} / ${SEQUENCE_TARGET}`;

if (sequenceCelebrationActive) {
celebrationMessage.textContent = `Sequence complete! ${SEQUENCE_TARGET} alternations achieved.`;
celebrationMessage.hidden = false;
} else if (showCelebration) {
celebrationMessage.textContent = `Congratulations! You clicked the button ${totalClicks} times.`;
celebrationMessage.hidden = false;
} else {
celebrationMessage.hidden = true;
}
celebrationMessage.hidden = !showCelebration;
}

button.addEventListener("click", () => {
function advanceSequence(clickedId) {
const expectedId = sequenceStep % 2 === 0 ? button.id : button2.id;
if (clickedId === expectedId) {
sequenceStep += 1;
} else if (clickedId === button.id) {
sequenceStep = 1;
} else {
sequenceStep = 0;
}

if (sequenceStep >= SEQUENCE_LENGTH) {
sequenceCelebrationActive = true;
sequenceStep = 0;
document.body.dispatchEvent(new CustomEvent("sequence:complete"));
} else {
sequenceCelebrationActive = false;
}
}

function handlePrimaryClick() {
totalClicks += 1;
celebrationClicks += 1;

Expand All @@ -42,9 +80,19 @@ button.addEventListener("click", () => {
celebrationClicks = 0;
}

advanceSequence(button.id);

window.localStorage.setItem(TOTAL_KEY, String(totalClicks));
window.localStorage.setItem(CYCLE_KEY, String(celebrationClicks));
renderState(shouldCelebrate);
});
}

function handleSecondaryClick() {
advanceSequence(button2.id);
renderState(false);
}

button.addEventListener("click", handlePrimaryClick);
button2.addEventListener("click", handleSecondaryClick);

renderState(false);