Skip to content

Commit 27c39b0

Browse files
frant1cfrant1c
authored andcommitted
fix: change getRandomQuestion
1 parent fcea0f0 commit 27c39b0

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/store/question.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { create } from "zustand"
22

33
interface QsStore {
44
question: [string, string][]
5+
lastIndex: number
56
setQuestion: (qsString: string) => void
67
getRandomQuestion: () => [string, string]
78
}
89

910
const useQsStore = create<QsStore>((set, get) => ({
1011
question: [],
12+
lastIndex: -1,
1113

1214
setQuestion: (qsString: string) => {
1315
if (!qsString?.trim()) {
@@ -47,7 +49,10 @@ const useQsStore = create<QsStore>((set, get) => ({
4749
getRandomQuestion: () => {
4850
const questions = get().question
4951
const count = questions.length
50-
const randomIndex = Math.floor(Math.random() * count)
52+
let randomIndex = Math.floor(Math.random() * count)
53+
while (randomIndex === get().lastIndex) {
54+
randomIndex = Math.floor(Math.random() * count)
55+
}
5156
console.log(questions[randomIndex])
5257
return questions[randomIndex]
5358
}

0 commit comments

Comments
 (0)