Repository navigation
Expand file tree
/
Copy pathhard.js
More file actions
171 lines (142 loc) · 5.42 KB
/
Copy pathhard.js
File metadata and controls
171 lines (142 loc) · 5.42 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// ---------------------------------------------------------
// Hi! Are you here to find the answer to Challenge 3?
// Scroll down to the "Read the JavaScript" section, your answer is hidden there!
// ---------------------------------------------------------
//
window.addEventListener("DOMContentLoaded", (event) => {
//
// ---------------------------------------------------------
// Set hard mode timer
// ---------------------------------------------------------
//
if (!localStorage.getItem("startTime")) {
localStorage.setItem("startTime", new Date().getTime());
};
let challengeTimer;
const timerEl = document.querySelector(".timer");
function formatTime(t) {
return t.toString().substring(0, 2).padStart(2, '0')
}
function updateTimer() {
let timeSinceStart = new Date(new Date().getTime() - localStorage.getItem("startTime"));
let minutes = formatTime(timeSinceStart.getMinutes());
let seconds = formatTime(timeSinceStart.getSeconds());
let milliseconds = formatTime(timeSinceStart.getMilliseconds());
timerEl.innerHTML = `${minutes}:${seconds}:${milliseconds}`;
if (timeSinceStart.getMinutes() >= 60) {
localStorage.removeItem("startTime");
localStorage.removeItem("endTime");
window.location.href = "/timeover.html";
}
};
setInterval(updateTimer);
const lastChallengeSubmitBtn = document.querySelector("button.styles-btn");
if (lastChallengeSubmitBtn) {
lastChallengeSubmitBtn.addEventListener("click", event => {
localStorage.setItem("endTime", Date.now());
});
};
//
// ---------------------------------------------------------
// Hidden password challenge
// ---------------------------------------------------------
//
const hiddenPasswordSubmitBtn = document.querySelector("button.hidden-password");
const hiddenPasswordField = document.querySelector("input.hidden-password");
if (hiddenPasswordSubmitBtn) {
hiddenPasswordSubmitBtn.addEventListener("click", event => {
if (hiddenPasswordField.value == hiddenPasswordField.getAttribute("data-password")) {
window.location.href = "/challenges/hard/bat.html";
} else {
throwError();
};
});
};
//
// ---------------------------------------------------------
// Disabled button challenge
// ---------------------------------------------------------
//
const disabledBtn = document.querySelector("button[disabled]");
if (disabledBtn) {
disabledBtn.addEventListener("click", event => {
window.location.href = "/challenges/hard/cow.html";
});
};
//
// ---------------------------------------------------------
// Read the JavaScript challenge
// ---------------------------------------------------------
//
if (document.body.id == "js-challenge") {
document.addEventListener("dblclick", function (e) {
window.location.href = "/challenges/hard/duck.html";
});
};
//
// ---------------------------------------------------------
// Local Storage challenge
// ---------------------------------------------------------
//
const storagePasswordSubmitBtn = document.querySelector("button.storage");
const storagePasswordField = document.querySelector("input.storage");
const storagePassword = "h0ud1n1";
if (storagePasswordSubmitBtn) {
localStorage.setItem("password", storagePassword);
storagePasswordSubmitBtn.addEventListener("click", event => {
const password = localStorage.getItem("password");
if (storagePasswordField.value == password) {
window.location.href = "/challenges/hard/gecko.html";
} else {
throwError();
}
});
} else {
localStorage.removeItem("password", storagePassword);
};
//
// ---------------------------------------------------------
// JavaScript Console challenge
// ---------------------------------------------------------
//
const consolePasswordSubmitBtn = document.querySelector("button.console");
const consolePasswordField = document.querySelector("input.console");
const consolePassword = `pr${1+2}st${0}`;
let clickCount = 0;
let clickMax = 3;
if (consolePasswordSubmitBtn) {
console.log("-------------------------------------------------")
console.log(`Click the submit button on this page ${clickMax - clickCount} times to receive the password.`)
consolePasswordSubmitBtn.addEventListener("click", event => {
if (consolePasswordField.value == consolePassword) {
window.location.href = "/challenges/hard/hare.html";
} else {
throwError();
if (clickCount >= 2) {
console.log(`The password is ${consolePassword}`)
} else {
clickCount++;
console.log(`Click the submit button on this page ${clickMax - clickCount} more times to receive the password.`)
};
};
});
};
});
//
// ---------------------------------------------------------
// Styles challenge
// ---------------------------------------------------------
//
const stylesPasswordSubmitBtn = document.querySelector("button.styles-btn");
const stylesPasswordField = document.querySelector("input.styles-password");
const stylesPassword = "hocus pocus"
if (stylesPasswordSubmitBtn) {
stylesPasswordSubmitBtn.addEventListener("click", event => {
if (stylesPasswordField.value == stylesPassword) {
window.location.href = "/challenges/hard/jellyfish.html";
} else {
throwError();
};
});
};