在特定条件下增加计数器(例如:仅当用户没有输入错误答案时)
increment a counter under certain condition (ex: only if user didn't input wrong answer)
现在,此 jsfiddle 中的代码会生成数学问题,当用户输入正确答案时,correct
计数器会增加。当用户输入 wrong
答案时,错误计数器会上升。我需要帮助更进一步。如果用户输入了错误的答案,正确的计数器就不能为该问题递增。他们必须得到正确的答案才能转到下一个问题,但是当他们回答正确时,正确的计数器不会增加。他们做错的那个问题只有错误的计数器递增一次。
我有时会卡在弄清楚这种类型的逻辑中:
pseudo Code:
attempt = 0 //set originally to 0 since question was attempted and answer was wrong
if(answer is right && attempt 0){
correct++
}else if(answer is wrong){
wrong++
attempt = 1 //question was attempted and answer was wrong
//this is where i get stuck. if i set attempt to 0 here it doesnt help
}
已编辑:每题做错也只算一次。
代码可以写成:
attempt = 0;
if(answer is right){
// First attempt is correct
if (attempt === 0) {
correct++
}
// go to next question, no matter the first try is correct or not.
// set attempts back to 0, for a new question.
attempt= 0;
}else if(answer is wrong){
// If we tried it, but wrong, then mark the answer wrong
if (attempt === 0) {
wrong++
}
attempt++
}
然后看看你修改后的逻辑 jsfiddle
现在,此 jsfiddle 中的代码会生成数学问题,当用户输入正确答案时,correct
计数器会增加。当用户输入 wrong
答案时,错误计数器会上升。我需要帮助更进一步。如果用户输入了错误的答案,正确的计数器就不能为该问题递增。他们必须得到正确的答案才能转到下一个问题,但是当他们回答正确时,正确的计数器不会增加。他们做错的那个问题只有错误的计数器递增一次。
我有时会卡在弄清楚这种类型的逻辑中:
pseudo Code:
attempt = 0 //set originally to 0 since question was attempted and answer was wrong
if(answer is right && attempt 0){
correct++
}else if(answer is wrong){
wrong++
attempt = 1 //question was attempted and answer was wrong
//this is where i get stuck. if i set attempt to 0 here it doesnt help
}
已编辑:每题做错也只算一次。
代码可以写成:
attempt = 0;
if(answer is right){
// First attempt is correct
if (attempt === 0) {
correct++
}
// go to next question, no matter the first try is correct or not.
// set attempts back to 0, for a new question.
attempt= 0;
}else if(answer is wrong){
// If we tried it, but wrong, then mark the answer wrong
if (attempt === 0) {
wrong++
}
attempt++
}
然后看看你修改后的逻辑 jsfiddle