AskQuestion results in Undefined Response


#1

I have been following the Alexa course and I cannot figure out how to call a function within the same handle. Whenever I call this.AskQuestion in another function defined in the same handle, it always results in undefined in the JSON feedback. Below is the code (which is the same as the tutorial’s answer)

‘use strict’;

var Alexa = require(‘alexa-sdk’);

var flashcardsDictionary = [
{
state:“Alabama”,
capital:“Montgomery”
},
{
state:“Alaska”,
capital:“Juneau”
} // and many many others
];

var DECK_LENGTH = flashcardsDictionary.length;

var handlers = {

// Open Codecademy Flashcards
‘LaunchRequest’: function() {
this.response.speak(‘sup dude’ + Object.keys(this.attributes).length + numberCorrect + " and " + currentFlashcardIndex);

	if(Object.keys(this.attributes).length == 0) {
        this.attributes.flashcards = {
          'numberCorrect': 0,
          'currentFlashcardIndex': 0
        }

        this.response
 			.speak('Welcome to the sickist Flashcards ever on US states and capitals.'+this.AskQuestion)
            .listen('Welcome to the sickist Flashcards ever on US states and capitals.'+this.AskQuestion);

    } else {
        var numberCorrect = this.attributes.flashcards.numberCorrect;
        var currentFlashcardIndex = this.attributes.flashcards.currentFlashcardIndex;

        this.response
      	.speak('Welcome back to Flashcards. You\'re on question ' + currentFlashcardIndex +
        ' and have answered ' + numberCorrect + ' correctly.' + this.AskQuestion)
        .listen('Welcome back to Flashcards. You\'re on question ' + currentFlashcardIndex +
        ' and have answered ' + numberCorrect + ' correctly.' + this.AskQuestion);
    }
    this.emit(':responseReady');

},

// User gives an answer
‘AnswerIntent’: function() {
var currentFlashcardIndex = this.attributes.flashcards.currentFlashcardIndex;
var userAnswer = this.event.request.intent.slots.answer.value;
var correctAnswer = flashcardsDictionary[currentFlashcardIndex].capital;

if (userAnswer == correctAnswer){
    this.attributes.flashcards.numberCorrect++;
    var numberCorrect = this.attributes.flashcards.numberCorrect;
    this.response
      .speak('Nice job! The correct answer is ' + correctAnswer + '. You ' +
        'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' questions correct. Here is your next question. ' + this.AskQuestion)
      .listen(this.AskQuestion);
} else {
    var numberCorrect = this.attributes.flashcards.numberCorrect;
    this.response
      .speak('Sorry, the correct answer is ' + correctAnswer + '. You ' +
      'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' questions correct. Here is your next question.' + this.AskQuestion).listen(this.AskQuestion);
}

this.attributes.flashcards.currentFlashcardIndex++;
this.emit(':responseReady');

},

// Test my {language} knowledge
‘AskQuestion’: function() {
var currentFlashcardIndex = this.attributes.flashcards.currentFlashcardIndex;
var currentState = flashcardsDictionary[currentFlashcardIndex].state;

this.response.listen('What is the capital of ' + currentState);
this.emit(':responseReady');

},

// Stop
‘AMAZON.StopIntent’: function() {
this.response.speak(‘Ok, let’s play again soon.’);
this.emit(’:responseReady’);
},

// Cancel
‘AMAZON.CancelIntent’: function() {
this.response.speak(‘Ok, let’s play again soon.’);
this.emit(’:responseReady’);
},

// Save state
‘SessionEndedRequest’: function() {
console.log(‘session ended!’);
this.emit(’:saveState’, true);
}

};

exports.handler = function(event, context, callback){
var alexa = Alexa.handler(event, context, callback);
alexa.dynamoDBTableName = ‘CapitalFlashcards’;
alexa.registerHandlers(handlers);
alexa.execute();
};