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
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.
In the SetMyLanguageIntent
and AskQuestion
functions, replace the session attributes that are in the speech outputs with the variable language
.
In AnswerIntent
, save the user’s language:
- Underneath the
userAnswer
variable, save the'language'
session attribute to alanguage
variable. - Then create a
languageAnswer
variable that concatenateslanguage
and ‘Answer’.
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 theflashcardsDictionary
. Use thecurrentFlashcardIndex
variable to select the appropriate question, and thelanguageAnswer
variable to get the corresponding answer.
In both parts of the conditional statement within AnswerIntent
, create a variable called numberCorrect
and set the 'numberCorrect'
session attribute as its value.