1
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* 答题状态(顺序练习 / 答题卡共用)
|
||||
* 用 reassign 方式更新数组,保证 uvue 下响应式可靠触发。
|
||||
*/
|
||||
import { reactive } from 'vue'
|
||||
import { quizQuestions } from '@/mock/index.uts'
|
||||
|
||||
export type HrQuizState = {
|
||||
/** 每题已选选项下标,-1 表示未作答 */
|
||||
answers : number[]
|
||||
/** 是否标记 */
|
||||
marked : boolean[]
|
||||
/** 当前题号(从 0 开始) */
|
||||
current : number
|
||||
/** 是否已交卷 */
|
||||
submitted : boolean
|
||||
}
|
||||
|
||||
function emptyAnswers() : number[] {
|
||||
const arr : number[] = []
|
||||
for (let i = 0; i < quizQuestions.length; i++) { arr.push(-1) }
|
||||
return arr
|
||||
}
|
||||
|
||||
function emptyMarks() : boolean[] {
|
||||
const arr : boolean[] = []
|
||||
for (let i = 0; i < quizQuestions.length; i++) { arr.push(false) }
|
||||
return arr
|
||||
}
|
||||
|
||||
export const quizState = reactive({
|
||||
answers: emptyAnswers(),
|
||||
marked: emptyMarks(),
|
||||
current: 0,
|
||||
submitted: false
|
||||
} as HrQuizState)
|
||||
|
||||
/** 设置某题答案 */
|
||||
export function setAnswer(index : number, option : number) : void {
|
||||
const arr : number[] = []
|
||||
for (let i = 0; i < quizState.answers.length; i++) {
|
||||
arr.push(i == index ? option : quizState.answers[i])
|
||||
}
|
||||
quizState.answers = arr
|
||||
}
|
||||
|
||||
/** 切换标记 */
|
||||
export function toggleMark(index : number) : void {
|
||||
const arr : boolean[] = []
|
||||
for (let i = 0; i < quizState.marked.length; i++) {
|
||||
arr.push(i == index ? !quizState.marked[i] : quizState.marked[i])
|
||||
}
|
||||
quizState.marked = arr
|
||||
}
|
||||
|
||||
/** 已答数量 */
|
||||
export function answeredCount() : number {
|
||||
let n = 0
|
||||
for (let i = 0; i < quizState.answers.length; i++) {
|
||||
if (quizState.answers[i] >= 0) { n++ }
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
/** 标记数量 */
|
||||
export function markedCount() : number {
|
||||
let n = 0
|
||||
for (let i = 0; i < quizState.marked.length; i++) {
|
||||
if (quizState.marked[i]) { n++ }
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
/** 重置 */
|
||||
export function resetQuiz() : void {
|
||||
quizState.answers = emptyAnswers()
|
||||
quizState.marked = emptyMarks()
|
||||
quizState.current = 0
|
||||
quizState.submitted = false
|
||||
}
|
||||
Reference in New Issue
Block a user