0% found this document useful (0 votes)
14 views2 pages

Index JS

The document defines functions for a quiz app built with MiniPrograms. It initializes data from a question collection, displays one question at a time and tracks the number of correct answers. When all questions are answered it submits the score to a rank collection.

Uploaded by

georgeleong4646
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Index JS

The document defines functions for a quiz app built with MiniPrograms. It initializes data from a question collection, displays one question at a time and tracks the number of correct answers. When all questions are answered it submits the score to a rank collection.

Uploaded by

georgeleong4646
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const db = wx.cloud.

database()
const QDB = db.collection('question')
const RankDB = db.collection('rank')
Page({
data: {
question: "",
op0:'',
op1:'',
op2:'',
op3:'',
op4:'',
right:0,
nickName: "test",
questionDB:[],
nowQuestion:0
},

jumpPage() {
wx.navigateTo({
url: `/pages/rank/index?`
});
},
start(){
let that = this
QDB.get().then((res)=>{
that.setData({
questionDB: res.data
})
that.showQuestion()
})
},
showQuestion(){
console.log('刷新题目')
this.setData({
question: this.data.questionDB[this.data.nowQuestion].question,
op0:this.data.questionDB[this.data.nowQuestion].op[0],
op1:this.data.questionDB[this.data.nowQuestion].op[1],
op2:this.data.questionDB[this.data.nowQuestion].op[2],
op3:this.data.questionDB[this.data.nowQuestion].op[3]
})
},
choice(e){
let that = this
let num = e.currentTarget.dataset['index'] - 0
if(num==this.data.questionDB[this.data.nowQuestion].answer){
console.log('答对了');
this.data.right++
this.setData({
right:this.data.right
})
}
this.data.nowQuestion++
this.setData({
nowQuestion:this.data.nowQuestion
})
console.log(this.data.nowQuestion)
if ( this.data.nowQuestion< this.data.questionDB.length) {
this.showQuestion()
}else{
console.log('提交 rank 记录')
this.setData({
question: "",
op0:'',
op1:'',
op2:'',
op3:''
})
RankDB.add({
data:{
nickName: that.data.nickName,
score:that.data.right
}}).then(res => {
console.log(res)
}) .catch(console.error)
}
}
});

You might also like