Learn
Session Attributes
Session Attributes and Variables

In the last exercise, we used interpolation to create a speech output. Though, accessing session attributes and slot values can get messy. In this exercise we’ll see how variables can help us clean up our code.

Look at how our speech output changes when we add a variable to our intent:

'CurrentGenreIntent': function() { var genre = this.event.request.intent.slots.genres.value; this.attributes['genre'] = genre; this.response.listen('Ok, so you like ' + genre + ' movies. Let me ask you a few ' + 'more questions and then I’ll give you ' + 'my recommendation."); this.emit(':responseReady'); }

By saving our session attributes to the genre variable, we have refactored the string in our speech output to be more readable.

Instructions

1.

In the SetMyLanguageIntent and AskQuestion functions, save the user’s language:

In both functions, create a variable called language and set it equal to the 'language' session attribute.

2.

In the SetMyLanguageIntent and AskQuestion functions, replace the session attributes that are in the speech outputs with the variable language.

3.

In AnswerIntent, save the user’s language:

  • Underneath the userAnswer variable, save the 'language' session attribute to a language variable.
  • Then create a languageAnswer variable that concatenates language and ‘Answer’.
4.

Underneath the languageAnswer variable, create two new variables called currentFlashcardIndex and correctAnswer.

  • Set currentFlashcardIndex equal to the current flashcard index attribute.
  • Set correctAnswer variable equal to the correct answer for the current language. You’ll need to make a call to the flashcardsDictionary. Use the currentFlashcardIndex variable to select the appropriate question, and the languageAnswer variable to get the corresponding answer.
5.

In both parts of the conditional statement within AnswerIntent, create a variable called numberCorrect and set the 'numberCorrect' session attribute as its value.

Folder Icon

Take this course for free

Already have an account?