I have the following issue: I am currently working on a Chatbot that is supposed to simulate an exam situation. Therefore I let it ask questions and the User should have an x amount of time in order to answer the question. Once the time is up, the Bot is supposed to ask the next question. However, once the student has sent the answer ( by pressing the enter key -> because that is the only way to continue the conversation) the bot should interrupt & reset the timer as well and ask a new question.
I have the developed the following concept
It seems to work once - so basically skipping from question 1 to question 2 but after another Enter event, it returns the next two questions and so on.
What am I don't wrong here?
const Question1E = " 1-This is the easy Question";
const Question1H = " 1-This is the hard Question";
const Question2E = " 2-This is the easy Question";
const Question2H = " 2-This is the hard Question";
const Question3E = " 3-This is the easy Question";
const Question3H = " 3-This is the hard Question";
const Question4E = " 4-This is the easy Question";
const Question4H = " 4-This is the hard Question";
const Question5E = " 5-This is the easy Question";
const Question5H = " 5-This is the hard Question";
const Question6E = " 6-This is the easy Question";
const Question6H = " 6-This is the hard Question";
const Done = " DONE ";
let count = 1;
let question = null;
const scores = [];
const questions = {
1: {
intent: "Intent1",
easy: Question1E,
hard: Question1H,
},
2: {
intent: "Intent2",
easy: Question2E,
hard: Question2H,
},
3: {
intent: "Intent3",
easy: Question3E,
hard: Question3H,
},
4: {
intent: "Intent4",
easy: Question4E,
hard: Question4H,
},
5: {
intent: "Intent5",
easy: Question5E,
hard: Question5H,
},
6: {
intent: "Intent6",
easy: Question6E,
hard: Question6H,
},
};
function test() {
const timer = setTimeout(() => {
test();
}, 10000);
const enter = document.addEventListener("keydown", function(e) {
if (e.key === "Enter") {
clearTimeout(timer);
test();
}
});
if (count < 6) {
const currentquestion = questions[count];
//IGNORE THIS PART
if (count <= 1) {
let randomizer = Math.random();
randomizer <= 0.5 ?
(question = currentquestion.easy) :
(question = currentquestion.hard);
} else {
scores[scores.length - 1] >= 0.65 ?
(question = currentquestion.hard) :
(question = currentquestion.easy);
}
//CONTINUE HERE
console.log(`${question}`);
} else {
clearTimeout(timer);
console.log(`${Done}`);
}
count ;
}
test();
CodePudding user response:
- timer needs to be outside the function
- test needs to be its own function
let timer;
const test = () => {
if (count < 6) {
const currentquestion = questions[count];
//IGNORE THIS PART
if (count <= 1) {
let randomizer = Math.random();
randomizer <= 0.5 ?
(question = currentquestion.easy) :
(question = currentquestion.hard);
} else {
scores[scores.length - 1] >= 0.65 ?
(question = currentquestion.hard) :
(question = currentquestion.easy);
}
//CONTINUE HERE
console.log(`${question}`);
timer = setTimeout(test, 2000); // set it to something shorter while testing
} else {
clearTimeout(timer);
console.log(Done);
}
count ;
};
const enter = document.addEventListener("keydown", function(e) {
if (e.key === "Enter") {
clearTimeout(timer);
test()
}
});
test(); // start
<script>
// moved the setup
const Question1E = " 1-This is the easy Question";
const Question1H = " 1-This is the hard Question";
const Question2E = " 2-This is the easy Question";
const Question2H = " 2-This is the hard Question";
const Question3E = " 3-This is the easy Question";
const Question3H = " 3-This is the hard Question";
const Question4E = " 4-This is the easy Question";
const Question4H = " 4-This is the hard Question";
const Question5E = " 5-This is the easy Question";
const Question5H = " 5-This is the hard Question";
const Question6E = " 6-This is the easy Question";
const Question6H = " 6-This is the hard Question";
const Done = " DONE ";
let count = 1;
let question = null;
const scores = [];
const questions = {
1: {
intent: "Intent1",
easy: Question1E,
hard: Question1H,
},
2: {
intent: "Intent2",
easy: Question2E,
hard: Question2H,
},
3: {
intent: "Intent3",
easy: Question3E,
hard: Question3H,
},
4: {
intent: "Intent4",
easy: Question4E,
hard: Question4H,
},
5: {
intent: "Intent5",
easy: Question5E,
hard: Question5H,
},
6: {
intent: "Intent6",
easy: Question6E,
hard: Question6H,
},
};
</script>