-
Notifications
You must be signed in to change notification settings - Fork 0
/
pick_random_question.js
46 lines (40 loc) · 1.33 KB
/
pick_random_question.js
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
import question from "./question.js";
import questions from "./questions.js";
let questionNumber = 0;
const url = new URL(window.location);
function updateQueryString(value) {
const url = new URL(window.location);
url.searchParams.set(`q${value}`, value);
window.history.pushState("", "", url.toString());
}
export function pickRandomQuestion(questions) {
if (!window.location.search) {
let questionRandom = Math.floor(Math.random() * questions.length);
if (
+localStorage.getItem("que_lyu") !== questionRandom ||
!localStorage.getItem("que_lyu")
) {
questionNumber = questionRandom;
}
if (+localStorage.getItem("que_lyu") === questionRandom) {
pickRandomQuestion(questions);
}
} else {
let questionRandom = Math.floor(Math.random() * questions.length);
const url = new URL(window.location);
if (url.searchParams.get(`q${questionRandom}`) == null) {
questionNumber = questionRandom;
}
if (url.searchParams.get(`q${questionRandom}`)) {
pickRandomQuestion(questions);
}
}
}
export function checkAnswerNumber(questions) {
pickRandomQuestion(questions);
question.textContent = questions[questionNumber].question;
updateQueryString(questionNumber);
localStorage.setItem("que_lyu", questionNumber);
}
checkAnswerNumber(questions);
export default questionNumber;