so I have a developing a trivia game and I have am trying to have a function redirect to a page that tells them if the answer was correct or not. Below shows what I have going on.
My functions to check the answers that the end user inputed.
function Question(stem, answer, type){
this.stem = stem;
this.answer = new Array(rowLimit);
this.answer.push(answer);
this.type = type;
}
function score(userAnswer){
var result = "";
console.log("Data returning for this.answer: " + this.answer);
var lowerUserAnswer = userAnswer.toLowerCase().trim();
for(var a = 2; a < this.answer.length; a++){
lowerAnswer = this.answer[a].trim().toLowerCase();
if (type == 1) { // this is a true / false question
console.log('Inside type 1');
result = trueFalseResponses(lowerUserAnswer, lowerAnswer);
} else if (type == 2) {// single word question
console.log('Inside type 2');
result = singleWordResponses(lowerUserAnswer, lowerAnswer);
} else if (type == 3){ // sentence question
console.log('Inside type 3');
result = sentenceResponses(lowerUserAnswer, lowerAnswer);
} else {
console.log('Inside of else');
console.log("Question type is needed.");
}
return result;
}
}
function trueFalseResponses(userInput, realAnswer){
if(userInput.charAt(0) == realAnswer.charAt(0)) {
console.log('Inside of if trueFalseResponses');
correctAnswerIncrease();
}
else {
console.log('Inside of else trueFalseResponses');
incorrectAnswerIncrease();
}
return "";
}
function singleWordResponses(userInput, realAnswer){
if(userInput === realAnswer) {
console.log('Inside of if singleWordResponses');
correctAnswerIncrease();
}
else {
console.log('Inside of else singleWordResponses');
incorrectAnswerIncrease();
}
return "";
}
function sentenceResponses(userInput, realAnswer){
if(userInput.includes(realAnswer)) {
console.log('Inside of if sentenceResponses');
correctAnswerIncrease();
}
else {
console.log('Inside of else sentenceResponses');
incorrectAnswerIncrease();
}
return "";
}
function correctAnswerIncrease (req, res, next){
console.log('Inside of if correctAnswerIncrease');
incremented = incremented + 1;
soulTracker = soulTracker + 1;
res.redirect('/correct');
}
function incorrectAnswerIncrease(req, res, next){
console.log('Inside of if incorrectAnswerIncrease');
res.send('/incorrect');
incremented = incremented + 1;
Web pages that tells them if the response was correct or not…
app.get('/correct', (req, res, next) => {
res.render('correct');
});
app.get('/incorrect', (req, res, next) => {
res.render('incorrect');
});
app.post('/correct', (req, res, next) => {
res.render('correct');
});
app.post('/incorrect', (req, res, next) => {
res.render('incorrect');
});
So when the answer is inputed, depending on the type of response the question can take, it’s passed from score, to the question type which is either trueFalseResponses, singleWordResponses, or sentenceResponses. If the response was correct, or incorrect, I want the page to redirect them to /correct or /incorrect. However, when I attempted that, I received the following error.
TypeError: Cannot read properties of undefined (reading ‘redirect’)
at correctAnswerIncrease (/Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/catchingSoulsServer.js:244:9)
at trueFalseResponses (/Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/catchingSoulsServer.js:210:9)
at score (/Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/catchingSoulsServer.js:193:22)
at /Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/catchingSoulsServer.js:440:5
at Layer.handle [as handle_request] (/Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/node_modules/express/lib/router/layer.js:95:5)
at /Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/jonathandameus/Library/CloudStorage/OneDrive-Personal/Business Files/Jon Does Tech/Code Development/Personal Project/VisualStudio/Bible Trivia/node_modules/express/lib/router/index.js:341:12)
I have tried to use window.location.href, window.location.href and window.location.replace but that has not helped as well. The goal is to redirect them to /correct or /incorrect if the response from the user was correct or incorrect.