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')
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.
CodePudding user response:
In this code:
function sentenceResponses(userInput, realAnswer){
if(userInput.includes(realAnswer)) {
console.log('Inside of if sentenceResponses');
correctAnswerIncrease();
}
else {
console.log('Inside of else sentenceResponses');
incorrectAnswerIncrease();
}
return "";
}
You call either correctAnswerIncrease()
or incorrectAnswerIncrease()
, but those two functions expect multiple parameters to be passed to them:
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;
}
Thus, when you try to use res
inside those functions, you get an error because the caller didn't pass res
and thus they are undefined
. When you call a function, you MUST pass the arguments that the function expects.
In addition incorrectAnswerIncrease()
probably should be res.redirect('/incorrect')
, not res.send('/incorrect')
.
I might also mention that it appears you're using server-wide variables here like incremented
and soulTracker
. That will work only if all users are supposed to share the same variables. If these are supposed to be user-specific variables, this will not work as all users will be sharing the same variables.
CodePudding user response:
I ended up restructuring the code by making the following changes.
The codes below became booleans.
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.");
};
console.log('Result from score is ' result);
if (result === true) {
soulTracker = 1;
incremented = 1;
} else {
incremented = 1;
};
return result;
}}
function trueFalseResponses(userInput, realAnswer){
if(userInput.charAt(0) == realAnswer.charAt(0)) {
console.log('Inside of if trueFalseResponses');
return true;
} else {
console.log('Inside of else trueFalseResponses');
return false;
}}
function singleWordResponses(userInput, realAnswer){
if(userInput === realAnswer) {
console.log('Inside of if singleWordResponses');
return true;
} else {
console.log('Inside of else singleWordResponses');
return false;
}}
function sentenceResponses(userInput, realAnswer){
if(userInput.includes(realAnswer)) {
console.log('Inside of if sentenceResponses');
return true;
} else {
console.log('Inside of else sentenceResponses');
return false;
}}
I also made the following changes to my post method.
app.post('/triviaquestions', (req, res, next) => {
if (score(req.body.triviaResponse) === true){
console.log('Inside of if score = true');
res.redirect('/correct');
}
else {
console.log('Inside of if score = false');
res.redirect('/incorrect');
}});
These changes turned out to be successful.