-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (104 loc) · 4.53 KB
/
script.js
File metadata and controls
132 lines (104 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// ============================================================================
// 1. GET REFERENCES TO HTML ELEMENTS
// ============================================================================
const userQuestionInput = document.getElementById('user-question');
const choiceContainer = document.getElementById('choice-container');
const thinkingContainer = document.getElementById('thinking-container');
const resultContainer = document.getElementById('result-container');
const yesButton = document.getElementById('yes-button');
const noButton = document.getElementById('no-button');
const decideButton = document.getElementById('decide-button');
const resultText = document.getElementById('result-text');
// ============================================================================
// 2. DATA / STATE
// ============================================================================
// The list of random placeholder suggestions that appear on focus.
const placeholderSuggestions = [
"the void answer truthfully",
"the stars align",
"I am on the right path",
"things be okay",
"I find what I'm looking for",
"a new door open",
"the message be received",
"my universe expand"
];
// ============================================================================
// 3. CORE FUNCTIONS
// ============================================================================
/**
* Runs when the user makes a final choice ("Yes" or "No").
* It hides the choices, shows the thinking state, and then reveals the result.
*/
function makeChoice() {
// First, remove any highlight effect from the buttons.
yesButton.classList.remove('highlight');
noButton.classList.remove('highlight');
// Hide the main choice interface.
choiceContainer.classList.add('hidden');
// Show the "Gazing into the void..." state.
thinkingContainer.classList.remove('hidden');
// Wait for 2 seconds before showing the final result.
setTimeout(() => {
thinkingContainer.classList.add('hidden');
// Randomly determine the outcome (a 50/50 chance).
const result = Math.random() < 0.5 ? 'Yes.' : 'No.';
// Display the result.
resultText.textContent = result;
resultContainer.classList.remove('hidden');
}, 2000); // 2000ms = 2 seconds
}
/**
* Resets the entire interface to its initial state for a new question.
*/
function reset() {
resultContainer.classList.add('hidden');
choiceContainer.classList.remove('hidden');
// Clear the user's previous question.
userQuestionInput.value = '';
// Reset the placeholder back to its mysterious initial state.
userQuestionInput.placeholder = '...';
// Automatically focus the input for a smooth user experience.
userQuestionInput.focus();
}
/**
* Provides a random suggestion by highlighting either the "Yes" or "No" button.
*/
function helpMeDecide() {
// First, clear any highlights from a previous suggestion.
yesButton.classList.remove('highlight');
noButton.classList.remove('highlight');
// Randomly choose which button to highlight.
const choice = Math.random() < 0.5 ? yesButton : noButton;
choice.classList.add('highlight');
// The highlight is temporary. It fades after 3 seconds to prompt the user to act.
setTimeout(() => {
choice.classList.remove('highlight');
}, 3000); // 3000ms = 3 seconds
}
// ============================================================================
// 4. EVENT LISTENERS (Attaching functions to user actions)
// ============================================================================
// --- Main Action Buttons ---
yesButton.addEventListener('click', makeChoice);
noButton.addEventListener('click', makeChoice);
decideButton.addEventListener('click', helpMeDecide);
resultContainer.addEventListener('click', reset);
// --- Interactive Placeholder Logic ---
// When the user clicks INTO the input box...
userQuestionInput.addEventListener('focus', () => {
const randomIndex = Math.floor(Math.random() * placeholderSuggestions.length);
userQuestionInput.placeholder = placeholderSuggestions[randomIndex];
});
// When the user clicks AWAY from the input box...
userQuestionInput.addEventListener('blur', () => {
// If they didn't type anything, reset the placeholder back to "...".
if (userQuestionInput.value === '') {
userQuestionInput.placeholder = '...';
}
});
// --- On Page Load ---
// When the page first loads, automatically focus the cursor in the input box.
window.onload = () => {
userQuestionInput.focus();
};